perm filename TANGLX.TEX[WEB,ALS] blob sn#621852 filedate 1981-11-03 generic text, type T, neo UTF8
\input webhdr

% Here is TEX material that gets inserted after \input webhdr
\def\hang{\hangindent 3em\ \unskip\!}
\chcode'27=13 \def↔{\ifmmode{\mathrel{\char'444}}\else{\penalty999\ } }
	% make ↔ tie two words together except in math mode
\def\TEX{\hbox{T\hskip-.1667em\lower.424ex\hbox{E}\hskip-.125em X}}
\font b=cmr9 \def\mc{\:b} % medium caps for names like PASCAL
\def\PASCAL{{\mc PAS}\-{\mc CAL}}
\def\pb{$\.|\ldotsm\.|$} % pascal brackets (|...|)
\def\v{\.{\char'174}} % vertical (|) in typewriter font

\font D=cmtt at 15truept % font used in the title line below (only)
\font E=cmr7 at 14truept % font used in the title line below (only)
\topspace 0pt
\vfill
\ctrline{\:E The {\:D TANGLE} processor}
\vskip 15pt
\ctrline{(preliminary version, November 1981)}
\vfill
\ctrline{\baselineskip9pt\hbox par 5in{\:bThis copy of the program is
	being distributed to interested ``guinea pigs'' before it has
	been thoroughly tested, in order to get extra help in the testing
	process. It is expected that changes will be made frequently
	while \TEX82 is being developed in the next months. Several
	comments in this documentation imply that the program has
	already been applied successfully to \TEX; such remarks are
	merely 	wishful thinking at this point, but they will be true
	when the final version is completed.}}
\mark{1}\eject
\N1.  The TANGLE processor.
This program converts a \.{WEB} file to a \PASCAL\ file. It was written
by D. E. Knuth in September, 1981; a somewhat similar {\sc SAIL}
program had
been developed in March, 1979. Since this program describes itself, a
bootstrapping process involving hand-translation had to be used to get
started.

For large \.{WEB} files one should have a large memory, since \.
{TANGLE} keeps
all the \PASCAL\ text in memory (in an abbreviated form). The program
uses
a few features of the local \PASCAL\ compiler that may need to be
changed in
other installations:

\yskip\item{1)} Case statements have a default.
\item{2)} Input-output is done with ascii characters in a way that
allows
end-of-page marks to be distinguished from end-of-line marks.

\yskip\noindent
These features are also present in the \PASCAL\ version of \TEX, where
they
are used in a similar (but more complex) way. System-dependent portions
of \.{TANGLE} can be identified by looking at the entries for `system
dependencies' in the index below.


\M2. The program begins with a fairly normal header, made up of pieces
that
will mostly be filled in later. The \.{WEB} input comes from file \\
{input},
the \PASCAL\ output goes to file \\{output}, the string pool output
goes to file \\{pool}, and error messages go to the terminal (\\{tty}).

If it is necessary to abort the job because of a fatal error, the
program
calls the `\\{quit}' procedure, which goes to the label \\
{end_of_TANGLE}.

\Y\P\D \37$\\{end_of_TANGLE}=9999$\C{go here to wrap it up}\par
\Y\P\X4:Compiler directives\X \6
\4\&{program}\1\  \37$\\{TANGLE}(\\{input},\39\\{output},\39\\{pool},\3
9\\{tty})$;\6
\4\&{label} \37\\{end_of_TANGLE};\C{go here to finish}\6
\4\&{const} \X7:Constants in the outer block\X \6
\4\&{type} \X11:Types in the outer block\X \6
\4\&{var}  \X12:Globals in the outer block\X\X17:Error handling
procedures\X \6
\4\&{procedure}\1\  \37\\{initialize}; \6
\4\&{var}  \X38:Local variables for initialization\X \6
\&{begin} \37\X13:Set initial values\X\6
\&{end};\par

\M3. Some of this code is optional for use when debugging only; such
material
is enclosed between the delimiters  \&{debug}  and $  \&{gubed} $. 
Other parts, delimited by  \&{stat}  and $  \&{tats} $,
are optionally included if statistics about \.{TANGLE}'s memory
usage are desired. Another small section of the program
is used to skip the `directory pages' in files produced with the {\mc
E} editor
at Stanford; that code is delimited by  \&{stanford}  and $  \&
{drofnats} $.

\Y\P\D \37$\\{debug}≡\B$\C{change this to `$\\{debug}\eqv\null$' when
debugging}\par
\P\D \37$\\{gubed}≡\T$\C{change this to `$\\{gubed}\eqv\null$' when
debugging}\par
\P\F \37$\\{debug}≡\\{begin}$\par
\P\F \37$\\{gubed}≡\\{end}$\Y\par
\P\D \37$\\{stat}≡$\C{change this to `$\\{stat}\eqv\null$' when
gathering usage statistics}\par
\P\D \37$\\{tats}≡$\C{change this to `$\\{tats}\eqv\null$' when
gathering usage statistics}\par
\P\F \37$\\{stat}≡\\{begin}$\par
\P\F \37$\\{tats}≡\\{end}$\Y\par
\P\D \37$\\{stanford}≡$\C{change this to `$\\{stanford}\eqv\.{@\{}$'
when not 	using {\mc E} files}\par
\P\D \37$\\{drofnats}≡$\C{change this to `$\\{drofnats}\eqv\.{@\}}$'
when not 	using {\mc E} files}\par
\P\F \37$\\{stanford}≡\\{begin}$\par
\P\F \37$\\{drofnats}≡\\{end}$\par

\M4. The \PASCAL\ compiler used to develop this system has `compiler
directives' that can appear in comments whose first character is a
dollar sign.
In production versions of \.{TANGLE} these directives tell the compiler
that
it is safe to avoid range checks and to leave out the extra code it
inserts
for the \PASCAL\ debugger's benefit, although interrupts will occur if
there is arithmetic overflow.

\Y\P$\4\X4:Compiler directives\X≡$\6
$\B\J\$\\C-,\39\\A+,\39\\D-\T$\C{no range check, catch arithmetic
overflow, no debug overhead}\6
$\\{DEBUG}\B\J\$\\C+,\39\\D+\T\\{GUBED}$\C{but turn everything on when
debugging}\par
\U section 2.

\M5. Labels are given symbolic names by the following definitions. We
insert
the label `\\{exit}:' just before the \&{end} of a procedure in which
we have
used the `\&{return}' statement defined below;
the label `\\{restart}' is occasionally used at the very beginning of a
procedure; and the label `\\{reswitch}' is occasionally used just prior
to
a \&{case} statement in which some cases change the conditions and we
wish to
branch to the newly applicable case.
Loops that are set up with the \&{loop} construction defined below are
commonly exited by going to `\\{done}' or to `\\{found}' or to `\\
{not_found}',
and they are sometimes repeated by going to `\\{continue}'.

\Y\P\D \37$\\{exit}=10$\C{go here to leave a procedure}\par
\P\D \37$\\{restart}=20$\C{go here to start a procedure again}\par
\P\D \37$\\{reswitch}=21$\C{go here to start a case statement again}\par
\P\D \37$\\{continue}=22$\C{go here to resume a loop}\par
\P\D \37$\\{done}=30$\C{go here to exit a loop}\par
\P\D \37$\\{found}=31$\C{go here when you've found it}\par
\P\D \37$\\{not_found}=32$\C{go here when you've found something
else}\par

\M6. Here are some macros for common programming idioms and for the
default
case statement.

\Y\P\D \37$\\{incr}(\#)≡\#←\#+1$\C{increase a variable by unity}\par
\P\D \37$\\{decr}(\#)≡\#←\#-1$\C{decrease a variable by unity}\par
\P\D $\\{loop}≡$ \6
\&{while} $\\{true}$ \1\&{do}\C{repeat over and over until a \&{goto} 
happens}\par
\P\D \37$\\{do_nothing}≡$\C{empty statement}\par
\P\D $\\{return}≡$ \&{goto} \37\\{exit}\C{terminate a procedure
call}\par
\P\D \37$\\{othercases}≡\\{others}$: \37\C{default for cases not listed
explicitly}\par
\P\D \37$\\{endcases}≡$ \6
\&{end} \C{follows the default case}\par
\P\F \37$\\{othercases}≡\\{else}$\par
\P\F \37$\\{endcases}≡\\{end}$\par
\P\F \37$\\{return}≡\\{nil}$\par
\P\F \37$\\{loop}≡\\{xclause}$\par

\M7. The following parameters are set big enough to handle \TEX, so
they
should be sufficient for most applications of \.{TANGLE}.

\Y\P$\4\X7:Constants in the outer block\X≡$\6
$\\{buf_size}=100$;\C{maximum length of input line}\6
$\\{max_bytes}=30000$;\C{number of bytes in identifiers, strings,
module names; 	must be less than 65536}\6
$\\{max_toks}=65535$;\C{number of bytes in compressed \PASCAL\ code;
	must be less than 65536}\6
$\\{max_names}=4000$;\C{number of identifiers, strings, module names;
	must be less than 10240}\6
$\\{max_texts}=2000$;\C{number of replacement texts, must be less than
10240}\6
$\\{hash_size}=353$;\C{should be prime}\6
$\\{longest_name}=300$;\C{module names shouldn't be longer than this}\6
$\\{line_length}=72$;\C{lines of \PASCAL\ output have at most this many
characters}\6
$\\{out_buf_size}=144$;\C{length of output buffer, should be twice \\
{line_length}}\6
$\\{stack_size}=50$;\C{number of simultaneous levels of macro
expansion}\6
$\\{max_id_length}=12$;\C{long identifiers are chopped to this length,
which must 	not exceed \\{line_length}}\6
$\\{unambig_length}=7$;\C{identifiers must be unique if chopped to this
length}\6
\C{note that 7 is more strict than \PASCAL's 8, but this can be
varied}\par

\U section 2.
\N8.  Input and output.
The input conventions of this program are intended to be very much like
those
of \TEX\ (except, of course, they are much simpler because much less
needs
to be done). Furthermore they are identical to those of \.{WEAVE}.
Therefore people who need to make modifications to all three systems
should be able to save some time and some headaches.

However, we use the standard \PASCAL\ input/output procedures here
wherever
possible, since \.{TANGLE} does not have to deal with files that are
named
dynamically by the user, and since there is no input from the terminal.


\M9. Terminal output is done by writing on file \\{tty}:

\Y\P\D \37$\\{print}(\#)≡\\{write}(\\{tty},\39\#)$\C{`\\{print}' means
write on the terminal}\par
\P\D \37$\\{print_ln}(\#)≡\\{write_ln}(\\{tty},\39\#)$\C{`\\{print}'
and then start new line}\par
\P\D \37$\\{print_nl}(\#)≡$\C{print information starting on a new
line}\6
\&{begin} \37$\\{write_ln}(\\{tty})$;\5
$\\{print}(\#)$;\6
\&{end}\par
\P\D \37$\\{ex_print_nl}(\#)≡\\{write_ln}(\\{tty})$;\5
$\\{print}(\#)$\par

\M10. The following code re-opens the \\{input} file in a mode that
(a)↔allows us
to see end-of-line characters, and (b)↔does not get the first
characters;
the first \\{read_ln} does the first \\{get}. The procedure returns \\
{true} if
the file could not be opened.

\Y\P\4\&{function}\1\  \37\\{open_input}: \37\\{boolean};\C{prepare for
ascii input on the \\{input} file}\2\6
\&{begin} \37$\\{reset}(\\{input},\39\.{\'\'},\39\.{\'/E/I/O\'})$;\5
$\\{open_input}←\\{eof}(\\{input})$;\6
\&{end};\par

\M11. The external text files we work with are of type \\{char}, which
is the full
7-bit ascii code on our local \PASCAL.
Internal calculations of \.{TANGLE} are, however, done entirely with
the
type \\{ascii_code}, which is a subrange of the integers. The \\{ord}
and \\{chr}
functions of \PASCAL\ are used to convert to and from \\{ascii_code}
numbers.

\Y\P$\4\X11:Types in the outer block\X≡$\6
$\\{ascii_file}=$ \&{file} \1\&{of}\5
\\{char};\C{text files}\2\6
$\\{ascii_code}=0\to127$;\C{seven-bit numbers}\par
\A sections 26, 28, 31, and 65.
\U section 2.

\M12. String pool constants are written to the \\{pool} file.

\Y\P$\4\X12:Globals in the outer block\X≡$\6
\4\\{pool}: \37\\{ascii_file};\par
\A sections 14, 16, 27, 29, 32, 37, 52, 57, 66, 68, 72, 80, 81, 86,
108, 116, 127, 134, 140, and 148.
\U section 2.

\M13. The following code opens \\{pool} and checks to make sure that
the external
file is available for writing.

\Y\P$\4\X13:Set initial values\X≡$\6
$\\{rewrite}(\\{pool},\39\.{\'\'},\39\.{\'/O\'})$;\6
\&{if} $¬\\{eof}(\\{pool})$ \1\&{then}\6
\&{begin} \37$\\{ex_print_nl}(\.{\'!\ Couldn\'}\.{\'t\ open\ the\ pool\
file.\'})$;\5
\\{quit};\6
\&{end};\2\par
\A sections 30, 33, 35, 39, 58, 123, and 149.
\U section 2.

\M14. Input goes into an array called \\{buffer}.

\Y\P$\4\X12:Globals in the outer block\X\mathrel{+}≡$\6
\4\\{buffer}: \37\&{array} $[0\to\\{buf_size}]$ \1\&{of}\5
\\{ascii_code};\2\par

\M15. The following procedure gets one line of input, according to the
conventions
of \TEX, namely: If the \\{input} file has been entirely read, \\
{input_ln} returns
\\{false} and does nothing else. Otherwise if the next item in the file
is an
end-of-page mark, the procedure sets $\\{buffer}[0]←\\{form_feed}$, $\\
{limit}←0$, and
returns \\{true}. Otherwise \\{ascii_code} numbers representing the
next line
of the file are input into $\\{buffer}[0]$, $\\{buffer}[1]$, $\ldotss$,
$\\{buffer}[\\{limit}-1]$; the global variable \\{limit} is set to the
length of the
line; $\\{buffer}[\\{limit}]$ is set to an ascii \\{carriage_return};
and the
procedure returns \\{true}.

\Y\P\D \37$\\{carriage_return}=\O15$\C{ascii code (control-M) used at
end of line}\par
\P\D \37$\\{form_feed}=\O14$\C{ascii code (control-L) used at end of
page}\par
\P\D \37$\\{tab_mark}=\O11$\C{ascii code (control-I) used as
tab-skip}\par
\Y\P\4\&{function}\1\  \37\\{input_ln}: \37\\{boolean};\C{inputs the
next line or returns \\{false}}\2\6
\&{begin} \37\\{read_ln};\C{\\{get} the first character of the line}\6
\&{if} $\\{eof}(\\{input})$ \1\&{then}\5
$\\{input_ln}←\\{false}$\6
\4\&{else} \&{begin} \37$\\{limit}←0$;\5
$\\{buffer}[0]←\\{ord}(\\{input}\↑)$;\6
\&{if} $\\{buffer}[0]≠\\{form_feed}$ \1\&{then}\C{not end of page}\6
\&{while} $\\{buffer}[\\{limit}]≠\\{carriage_return}$ \1\&{do}\6
\&{if} $\\{limit}=\\{buf_size}-1$ \1\&{then}\C{keep $\\{buffer}[\\
{buf_size}]$ empty}\6
\&{begin} \37$\\{buffer}[\\{limit}]←\\{carriage_return}$;\5
$\\{ex_print_nl}(\.{\'!\ Input\ line\ too\ long\'})$;\5
\\{error};\6
\&{end}\6
\4\&{else} \&{begin} \37$\\{incr}(\\{limit})$;\5
$\\{get}(\\{input})$;\6
\&{if} $\\{eof}(\\{input})$ \1\&{then}\5
$\\{buffer}[\\{limit}]←\\{carriage_return}$\6
\4\&{else} $\\{buffer}[\\{limit}]←\\{ord}(\\{input}\↑)$;\2\6
\&{end};\2\2\2\6
$\\{input_ln}←\\{true}$;\6
\&{end};\2\6
\&{end};\par
\N16.  Reporting errors to the user.
The \.{TANGLE} processor operates in two phases: first it inputs the
source
file and stores a compressed representation of the program, then it
produces
the \PASCAL\ output from the compressed representation. 

The global variable \\{phase_one} tells whether we are in Phase I or
not.

\Y\P$\4\X12:Globals in the outer block\X\mathrel{+}≡$\6
\4\\{phase_one}: \37\\{boolean};\C{\\{true} in Phase I, \\{false} in
Phase II}\par

\M17. If an error is detected while we are debugging,
we usually want to look at the contents of memory.
A special procedure will be declared later for this purpose.

\Y\P$\4\X17:Error handling procedures\X≡$\6
\\{DEBUG} \6
\4\&{procedure}\1\  \37\\{debug_help};\5
\\{forward};\5
\\{GUBED}\par
\A sections 18 and 23.
\U section 2.

\M18. During the first phase, syntax errors are reported to the user by
saying
$$\hbox{`$\\{err_print}(\.{\'!\ Error\ message\'})$'},$$
followed by `\\{quit}' if no recovery from the error is provided.
This will print the error message followed by an indication of where
the error
was spotted in the source file. Note that no period follows the error
message,
since the error routine will fill this in automatically.

Errors that are noticed during the second phase are reported to the
user
in the same fashion, but the error message will be
followed by an indication of where the error was spotted in the output
file.

The actual error indications are provided by a procedure called \\
{error}.

\Y\P\D $\\{err_print}(\#)≡$ \6
\&{begin} \37$\\{ex_print_nl}(\#)$;\5
\\{error};\6
\&{end}\par
\P\D \37$\\{ex_err_print}(\#)≡\\{ex_print_nl}(\#)$;\5
\\{error}\par
\Y\P$\4\X17:Error handling procedures\X\mathrel{+}≡$\6
\4\&{procedure}\1\  \37\\{error};\C{prints '\..' and location of error
message}\6
\4\&{var}  \X19:Local variables for \\{error}\X \6
\&{begin} \37\&{if} $\\{phase_one}$ \1\&{then}\5
\X20:Print error location based on input buffer\X\6
\4\&{else} \X22:Print error location based on output buffer\X;\2\6
\\{DEBUG}\\{debug_help};\5
\\{GUBED}\6
\&{end};\par

\M19. The error locations during Phase I can be indicated by using the
global
variables \\{loc}, \\{page}, and \\{line}, which tell respectively the
first
unlooked-at position in \\{buffer}, the current page number, and the
current
line number.

\Y\P$\4\X19:Local variables for \\{error}\X≡$\6
\4$\\k,\39\\l$: \37$0\to\\{buf_size}$;\C{indices into \\{buffer}}\par
\A section 21.
\U section 18.

\M20. \P$\X20:Print error location based on input buffer\X≡$\6
\&{begin} \37$\\{print_ln}(\.{\'.\ (p.\'},\39\\{page}:0,\39\.{\',l.\'
},\39\\{line}:0,\39\.{\')\'})$;\6
\&{if} $\\{loc}≥\\{limit}$ \1\&{then}\5
$\\l←\\{limit}$\6
\4\&{else} $\\l←\\{loc}$;\2\6
\&{for} $\\k←1\mathrel{\&{to}}\\l$ \1\&{do}\6
\&{if} $\\{buffer}[\\k-1]=\\{tab_mark}$ \1\&{then}\5
$\\{print}(\.{\'\ \'})$\6
\4\&{else} $\\{print}(\\{chr}(\\{buffer}[\\k-1]))$;\C{print the
characters already read}\2\2\6
$\\{print_ln}(\.{\'\'})$;\6
\&{for} $\\k←1\mathrel{\&{to}}\\l$ \1\&{do}\5
$\\{print}(\.{\'\ \'})$;\C{space out the next line}\2\6
\&{for} $\\k←\\l+1\mathrel{\&{to}}\\{limit}$ \1\&{do}\5
$\\{print}(\\{chr}(\\{buffer}[\\k-1]))$;\C{print the part not yet
read}\2\6
$\\{print}(\.{\'\ \'})$;\C{this space separates the message from future
page numbers}\6
\&{end}\par
\U section 18.

\M21. The position of errors detected during the second phase can be
indicated
by outputting the partially-filled output buffer, which contains \\
{out_ptr}
entries.

\Y\P$\4\X19:Local variables for \\{error}\X\mathrel{+}≡$\6
\4\\j: \37$0\to\\{out_buf_size}$;\C{index into \\{out_buf}}\par

\M22. \P$\X22:Print error location based on output buffer\X≡$\6
\&{begin} \37$\\{print_ln}(\.{\'.\ (l.\'},\39\\{line}:0,\39\.{\')\'
})$;\6
\&{for} $\\j←1\mathrel{\&{to}}\\{out_ptr}$ \1\&{do}\5
$\\{print}(\\{chr}(\\{out_buf}[\\j-1]))$;\C{print current partial
line}\2\6
$\\{print}(\.{\'...\'})$;\C{indicate that this information is partial}\6
\&{end}\par
\U section 18.

\M23. The \\{quit} procedure just cuts across all active procedure
levels
and jumps out of the program. This is the only non-local \&{goto}
statement
in \.{TANGLE}.

\Y\P$\4\X17:Error handling procedures\X\mathrel{+}≡$\6
\4\&{procedure}\1\  \37\\{quit};\2\6
\&{begin} \37\&{goto} \37\\{end_of_TANGLE};\6
\&{end};\par

\M24. Sometimes the program's behavior is far different from what it
should be,
and \.{TANGLE} prints an error message that is really for the \.
{TANGLE}
maintenance person, not the user. In such cases the program says
$\\{confusion}(\.{\'indication\ of\ where\ we\ are\'})$.

\Y\P\D $\\{confusion}(\#)≡$ \6
\&{begin} \37$\\{ex_err_print}(\.{\'!\ This\ can\'}\.{\'t\ happen\ (\'
},\39\#,\39\.{\')\'})$;\5
\\{quit};\6
\&{end}\par

\M25. An overflow stop occurs if \.{TANGLE}'s tables aren't large
enough.

\Y\P\D $\\{overflow}(\#)≡$ \6
\&{begin} \37$\\{ex_err_print}(\.{\'!\ Sorry,\ \'},\39\#,\39\.{\'\
capacity\ exceeded\'})$;\5
\\{quit};\6
\&{end}\par
\N26.  Data structures.
Most of the user's \PASCAL\ code is packed into seven- or eight-bit
integers
in two large arrays called \\{byte_mem} and \\{tok_mem}.
The \\{byte_mem} array holds the names of identifiers, strings, and
modules;
the \\{tok_mem} array holds the replacement texts
for macros and modules. Allocation is sequential, since things are
deleted only
during Phase II, and only in a last-in-first-out manner.

Auxiliary arrays \\{byte_start} and \\{tok_start} are used as
directories to
\\{byte_mem} and \\{tok_mem}, and the \\{link}, \\{ilk}, \\{equiv}, and
\\{text_link}
arrays give further information about names. These auxiliary arrays
consist of sixteen-bit items.

\Y\P$\4\X11:Types in the outer block\X\mathrel{+}≡$\6
$\\{eight_bits}=0\to255$;\C{unsigned one-byte quantity}\6
$\\{sixteen_bits}=0\to65535$;\C{unsigned two-byte quantity}\par

\M27. \P$\X12:Globals in the outer block\X\mathrel{+}≡$\6
\4\\{byte_mem}: \37\&{packed} \37\&{array} $[0\to\\{max_bytes}]$ \1\&
{of}\5
\\{ascii_code};\C{characters of names}\2\6
\4\\{tok_mem}: \37\&{packed} \37\&{array} $[0\to\\{max_toks}]$ \1\&
{of}\5
\\{eight_bits};\C{tokens}\2\6
\4\\{byte_start}: \37\&{array} $[0\to\\{max_names}]$ \1\&{of}\5
\\{sixteen_bits};\C{directory into \\{byte_mem}}\2\6
\4\\{tok_start}: \37\&{array} $[0\to\\{max_texts}]$ \1\&{of}\5
\\{sixteen_bits};\C{directory into \\{tok_mem}}\2\6
\4\\{link}: \37\&{array} $[0\to\\{max_names}]$ \1\&{of}\5
\\{sixteen_bits};\C{hash table or tree links}\2\6
\4\\{ilk}: \37\&{array} $[0\to\\{max_names}]$ \1\&{of}\5
\\{sixteen_bits};\C{type codes or tree links}\2\6
\4\\{equiv}: \37\&{array} $[0\to\\{max_names}]$ \1\&{of}\5
\\{sixteen_bits};\C{info corresponding to names}\2\6
\4\\{text_link}: \37\&{array} $[0\to\\{max_texts}]$ \1\&{of}\5
\\{sixteen_bits};\C{relates replacement texts}\2\par

\M28. The names of identifiers are found by computing a hash address \\
h and
then looking at strings of bytes signified by $\\{hash}[\\h]$, $\\
{link}[\\{hash}[\\h]]$,
$\\{link}[\\{link}[\\{hash}[\\h]]]$, $\ldotss$, until either finding
the desired name
or encountering a zero.

A `\\{name_pointer}' variable, which signifies a name, is an index into
\\{byte_start}. The actual sequence of characters in the name pointed
to by
$p$ appears in positions $\\{byte_start}[\\p]$ to $\\{byte_start}[\\
p+1]-1$, inclusive,
in \\{byte_mem}. The pointer 0 is used for undefined module names; we
don't
want to use it for the names of identifiers, since 0 stands for a null
pointer in a linked list.

Strings are treated like identifiers; the first character (a
double-quote)
distinguishes a string from an alphabetic name, but for \.{TANGLE}'s
purposes
strings behave like numeric macros. (A `string' here refers to the
strings delimited by double-quotes that \.{TANGLE} processes. \PASCAL\
string constants delimited by single-quote marks are not given such
special
treatment, they simply appear as sequences of characters in the
\PASCAL\
texts.)  The total number of strings in the string
pool is called \\{string_ptr}; the total number of names in \\
{byte_mem}
is called \\{name_ptr}; and the total number of bytes occupied in
\\{byte_mem} is called \\{byte_ptr}.

We usually have $\\{byte_start}[\\{name_ptr}]=\\{byte_ptr}$, since this
identifies the
end of the most recently created name, which is pointed to by $(\\
{name_ptr}-1)$.

\Y\P\D \37$\\{length}(\#)≡\\{byte_start}[\#+1]-\\{byte_start}[\#
]$\C{the length of a name}\par
\Y\P$\4\X11:Types in the outer block\X\mathrel{+}≡$\6
$\\{name_pointer}=0\to\\{max_names}$;\C{identifies a name}\par

\M29. \P$\X12:Globals in the outer block\X\mathrel{+}≡$\6
\4\\{name_ptr}: \37\\{name_pointer};\C{first unused position in \\
{byte_start}}\6
\4\\{string_ptr}: \37\\{name_pointer};\C{next number to be given to a
string of length $≠1$}\6
\4\\{byte_ptr}: \37$0\to\\{max_bytes}$;\C{first unused position in \\
{byte_mem}}\par

\M30. \P$\X13:Set initial values\X\mathrel{+}≡$\6
$\\{name_ptr}←1$;\5
$\\{string_ptr}←128$;\5
$\\{byte_ptr}←1$;\5
$\\{byte_start}[0]←1$;\5
$\\{byte_start}[1]←1$;\par

\M31. Replacement texts are stored in \\{tok_mem}, using similar
conventions.
A `\\{text_pointer}' variable is an index into \\{tok_start}, and the
replacement
text that corresponds to $p$
runs from positions $\\{tok_start}[\\p]$ to $\\{tok_start}[\\p+1]-1$,
inclusive.
Furthermore, $\\{text_link}[\\p]$ is used to connect pieces of text
that
have the same name, as we shall see later. The pointer 0 is used
for undefined replacement texts.

The first position of \\{tok_mem}
that is unoccupied by replacement text is called \\{tok_ptr}, and the
first
unused location of \\{tok_start} is called \\{text_ptr}.
We usually have $\\{tok_start}[\\{text_ptr}]=\\{tok_ptr}$, for the same
reason that
$\\{byte_start}[\\{name_ptr}]$ is usually equal to \\{byte_ptr}.

\Y\P$\4\X11:Types in the outer block\X\mathrel{+}≡$\6
$\\{text_pointer}=0\to\\{max_texts}$;\C{identifies a replacement
text}\par

\M32. \P$\X12:Globals in the outer block\X\mathrel{+}≡$\6
\4\\{text_ptr}: \37\\{text_pointer};\C{first unused position in \\
{tok_start}}\6
\4\\{tok_ptr}: \37$0\to\\{max_toks}$;\C{first unused position in \\
{tok_mem}}\6
\4\\{STAT}\\{max_tok_ptr}: \37$0\to\\{max_toks}$;\C{largest value
assumed by \\{tok_ptr}}\6
\\{TATS}\par

\M33. \P$\X13:Set initial values\X\mathrel{+}≡$\6
$\\{tok_ptr}←1$;\5
$\\{text_ptr}←1$;\5
$\\{tok_start}[0]←1$;\5
$\\{tok_start}[1]←1$;\par

\M34. Four types of identifiers are distinguished by their \\{ilk}:

\yskip\hang \\{normal} identifiers will appear in the \PASCAL\ program
as
ordinary identifiers since they have not been defined to be macros; the
\\{repl} field for such identifiers is a link in a secondary hash table
that
is used to check whether any two of them agree in their first \\
{unambig_length}
characters after underline symbols are removed and lower case letters
are
changed to upper case.

\yskip\hang \\{numeric} identifiers have been defined to be numeric
macros;
their \\{repl} field contains the corresponding numeric value plus
$2↑{15}$.
Strings are treated as numeric macros.

\yskip\hang \\{simple} identifiers have been defined to be simple
macros;
their \\{repl} field points to the corresponding replacement text.

\yskip\hang \\{parametric} identifiers have been defined to be
parametric macros;
like simple identifiers, their \\{repl} field points to the replacement
text.

\Y\P\D \37$\\{normal}=0$\C{ordinary identifiers have \\{normal} ilk}\par
\P\D \37$\\{numeric}=1$\C{numeric macros and strings have \\{numeric}
ilk}\par
\P\D \37$\\{simple}=2$\C{simple macros have \\{simple} ilk}\par
\P\D \37$\\{parametric}=3$\C{parametric macros have \\{parametric}
ilk}\par

\M35. The names of modules are stored in \\{byte_mem} together
with the identifier names, but a hash table is not used for them
because
\.{TANGLE} needs to be able to recognize a module name when given a
prefix of
that name. A conventional binary seach tree is used to retrieve module
names,
with fields called \\{llink} and \\{rlink} in place of \\{link} and \\
{ilk}. The
root of this tree is $\\{rlink}[0]$. If $p$ is a pointer to a module
name,
$\\{equiv}[\\p]$ points to its replacement text, just as in simple and
parametric
macros, unless this replacement text has not yet been defined (in which
case
$\\{equiv}[\\p]=0$).

\Y\P\D \37$\\{llink}≡\\{link}$\C{left link in binary search tree for
module names}\par
\P\D \37$\\{rlink}≡\\{ilk}$\C{right link in binary search tree for
module names}\par
\Y\P$\4\X13:Set initial values\X\mathrel{+}≡$\6
$\\{rlink}[0]←0$;\C{the binary search tree starts out with nothing in
it}\6
$\\{equiv}[0]←0$;\C{the undefined module has no replacement text}\par

\M36. Here is a little procedure that prints the text of a given name.

\Y\P\4\&{procedure}\1\  \37$\\{print_id}(\\p:\\
{name_pointer})$;\C{print identifier or module name}\6
\4\&{var} \37\\k: \37$0\to\\{max_bytes}$;\C{index into \\{byte_mem}}\2\6
\&{begin} \37\&{if} $\\p≥\\{name_ptr}$ \1\&{then}\5
$\\{print}(\.{\'IMPOSSIBLE\'})$\6
\4\&{else} \&{for} $\\k←\\{byte_start}[\\p]\mathrel{\&{to}}\\
{byte_start}[\\p+1]-1$ \1\&{do}\5
$\\{print}(\\{chr}(\\{byte_mem}[\\k]))$;\2\2\6
\&{end};\par
\N37.  Searching for identifiers.
The hash table described above is updated by the \\{id_lookup}
procedure,
which finds a given identifier and returns a pointer to its index in
\\{byte_start}. If the identifier was not already present, it is
inserted with
a given \\{ilk} code; and an error message is printed if the identifier
is being
doubly defined.

Because of the way \.{TANGLE}'s scanning mechanism works, it is most
convenient
to let \\{id_lookup} search for an identifier that is present in the \\
{buffer}
array. Two other global variables specify its position in the buffer:
the
first character is $\\{buffer}[\\{id_first}]$, and the last is $\\
{buffer}[\\{id_loc}-1]$.
Furthermore, if the identifier is really a string, the global variable
\\{double_chars} tells how many of the characters in the buffer appear
twice (namely \.{@@} and \.{""}), since this additional information
makes
it easy to calculate the true length of the string. The final
double-quote
of the string is not included in its ``identifier,'' but the first one
is,
so the string length is $\\{id_loc}-\\{id_first}-\\{double_chars}-1$.

We have mentioned that \\{normal} identifiers belong to two hash
tables,
one for their true names as they appear in the \.{WEB} file and the
other
when they have been reduced to their first \\{unambig_length}
characters.
The hash tables are kept by the method of simple chaining, where the
heads of the individual lists appear in the \\{hash} and \\{chop_hash}
arrays.
If \\h is a hash code, the primary hash table list starts at $\\
{hash}[\\h]$ and
proceeds through \\{link} pointers; the secondary hash table list
starts at
$\\{chop_hash}[\\h]$ and proceeds through \\{equiv} pointers. Of
course, the same
identifier will probably have two different values of \\h.

The \\{id_lookup} procedure uses an auxiliary array called \\
{chopped_id} to
contain up to \\{unambig_length} characters of the current identifier,
if
it is necessary to compute the secondary hash code. (This array could
be
declared local to \\{id_lookup}, but in general we are making all array
declarations global in this program, because some compilers and some
machine
architectures make dynamic array allocation inefficient.)

\Y\P$\4\X12:Globals in the outer block\X\mathrel{+}≡$\6
\4\\{id_first}: \37$0\to\\{buf_size}$;\C{where the current identifier
begins in the buffer}\6
\4\\{id_loc}: \37$0\to\\{buf_size}$;\C{just after the current
identifier in the buffer}\6
\4\\{double_chars}: \37$0\to\\{buf_size}$;\C{correction to length in
case of strings}\7
\4$\\{hash},\39\\{chop_hash}$: \37\&{array} $[0\to\\{hash_size}]$ \1\&
{of}\5
\\{sixteen_bits};\C{heads of hash lists}\2\6
\4\\{chopped_id}: \37\&{array} $[0\to\\{unambig_length}]$ \1\&{of}\5
\\{ascii_code};\C{chopped identifier}\2\par

\M38. Initially all the hash lists are empty.

\Y\P$\4\X38:Local variables for initialization\X≡$\6
\4\\h: \37$0\to\\{hash_size}$;\C{index into hash-head arrays}\par
\U section 2.

\M39. \P$\X13:Set initial values\X\mathrel{+}≡$\6
\&{for} $\\h←0\mathrel{\&{to}}\\{hash_size}-1$ \1\&{do}\6
\&{begin} \37$\\{hash}[\\h]←0$;\5
$\\{chop_hash}[\\h]←0$;\6
\&{end};\2\par

\M40. Here now is the main procedure for finding identifiers (and
strings).
The parameter $t$ is set to \\{normal} except when the identifier is
a macro name that is just being defined; in the latter case, $t$ will
be
\\{numeric}, \\{simple}, or \\{parametric}.

\Y\P\4\&{function}\1\  \37$\\{id_lookup}(\\t:\\{eight_bits})$: \37\\
{name_pointer};\C{finds current identifier}\6
\4\&{label} \37$\\{found},\39\\{not_found}$;\6
\4\&{var} \37\\c: \37\\{eight_bits};\C{byte being chopped}\6
\\i: \37$0\to\\{buf_size}$;\C{index into \\{buffer}}\6
\\h: \37$0\to\\{hash_size}$;\C{hash code}\6
\\k: \37$0\to\\{max_bytes}$;\C{index into \\{byte_mem}}\6
\\l: \37$0\to\\{buf_size}$;\C{length of the given identifier}\6
$\\p,\39\\q$: \37\\{name_pointer};\C{where the identifier is being
sought}\6
\\s: \37$0\to\\{unambig_length}$;\C{index into \\{chopped_id}}\2\6
\&{begin} \37$\\l←\\{id_loc}-\\{id_first}$;\C{compute the length}\6
\X41:Compute the hash code $h$\X;\6
\X42:Compute the name location $p$\X;\6
\&{if} $(\\p=\\{name_ptr})∨(\\t≠\\{normal})$ \1\&{then}\5
\X44:Update the tables and check for possible errors\X;\2\6
$\\{id_lookup}←\\p$;\6
\&{end};\par

\M41. A simple hash code is used: If the sequence of
ascii_codes is $c↓1c↓2\ldotsm c↓m$, its hash value will be
$$(2↑{n-1}c↓1+2↑{n-2}c↓2+\cdots+c↓n)\modop\\{hash_size}.$$

\Y\P$\4\X41:Compute the hash code $h$\X≡$\6
$\\h←\\{buffer}[\\{id_first}]$;\5
$\\i←\\{id_first}+1$;\6
\&{while} $\\i<\\{id_loc}$ \1\&{do}\6
\&{begin} \37$\\h←(\\h+\\h+\\{buffer}[\\i])\mathbin{\&{mod}}\\
{hash_size}$;\5
$\\{incr}(\\i)$;\6
\&{end}\2\par
\U section 40.

\M42. If the identifier is new, it will be placed in position $\\p=\\
{name_ptr}$, 
otherwise $p$ will point to its existing location.

\Y\P$\4\X42:Compute the name location $p$\X≡$\6
$\\p←\\{hash}[\\h]$;\6
\&{while} $\\p≠0$ \1\&{do}\6
\&{begin} \37\&{if} $\\{length}(\\p)=\\l$ \1\&{then}\5
\X43:Compare name $p$ with current identifier, \&{goto} \\{found} if
equal\X;\2\6
$\\p←\\{link}[\\p]$;\6
\&{end};\2\6
$\\p←\\{name_ptr}$;\C{the current identifier is new}\6
$\\{link}[\\p]←\\{hash}[\\h]$;\5
$\\{hash}[\\h]←\\p$;\C{insert $p$ at beginning of hash list}\6
\4\\{found}: \37\par
\U section 40.

\M43. \P$\X43:Compare name $p$ with current identifier, \&{goto} \\
{found} if equal\X≡$\6
\&{begin} \37$\\i←\\{id_first}$;\5
$\\k←\\{byte_start}[\\p]$;\6
\&{while} $(\\i<\\{id_loc})∧(\\{buffer}[\\i]=\\{byte_mem}[\\k])$ \1\&
{do}\6
\&{begin} \37$\\{incr}(\\i)$;\5
$\\{incr}(\\k)$;\6
\&{end};\2\6
\&{if} $\\i=\\{id_loc}$ \1\&{then}\5
\&{goto} \37\\{found};\C{all characters agree}\2\6
\&{end}\par
\U section 42.

\M44. \P$\X44:Update the tables and check for possible errors\X≡$\6
\&{begin} \37\&{if} $((\\p≠\\{name_ptr})∧(\\t≠\\{normal})∧(\\{ilk}[\\
p]=\\{normal}))∨((\\p=\\{name_ptr})∧(\\t=\\{normal})∧(\\{buffer}[\\
{id_first}]≠\.{""}\.{""}))$ \1\&{then}\5
\X45:Compute the secondary hash code $h$ and put the first characters
into the auxiliary array \\{chopped_id}\X;\2\6
\&{if} $\\p≠\\{name_ptr}$ \1\&{then}\5
\X46:Give double-definition error and change $p$ to type $t$\X\6
\4\&{else} \X48:Enter a new identifier into the table at position
$p$\X;\2\6
\&{end}\par
\U section 40.

\M45. The following routine, which is called into play when it is
necessary to
look at the secondary hash table, computes the same hash function as
before
(but on the chopped data), and places a zero after the chopped
identifier
in \\{chopped_id} to serve as a convenient sentinel.

\Y\P$\4\X45:Compute the secondary hash code $h$ and put the first
characters into the auxiliary array \\{chopped_id}\X≡$\6
\&{begin} \37$\\i←\\{id_first}$;\5
$\\s←0$;\5
$\\h←0$;\6
\&{while} $(\\i<\\{id_loc})∧(\\s<\\{unambig_length})$ \1\&{do}\6
\&{begin} \37\&{if} $\\{buffer}[\\i]≠\.{"_"}$ \1\&{then}\6
\&{begin} \37\&{if} $\\{buffer}[\\i]≥\.{"a"}$ \1\&{then}\5
$\\{chopped_id}[\\s]←\\{buffer}[\\i]-\O40$\6
\4\&{else} $\\{chopped_id}[\\s]←\\{buffer}[\\i]$;\2\6
$\\h←(\\h+\\h+\\{chopped_id}[\\s])\mathbin{\&{mod}}\\{hash_size}$;\5
$\\{incr}(\\s)$;\6
\&{end};\2\6
$\\{incr}(\\i)$;\6
\&{end};\2\6
$\\{chopped_id}[\\s]←0$;\6
\&{end}\par
\U section 44.

\M46. If a macro has appeared before it was defined, \.{TANGLE} will
still work all right; after all, such behavior is typical of the
replacement
texts for modules, which act very much like macros. However, an
undefined
numeric macro
may not be used on the right-hand side of another numeric macro
definition,
so \.{TANGLE} finds it simplest to make a blanket rule that macros
should
be defined before they are used. The following routine gives an error
message
and also fixes up any damage that may have been caused.

\Y\P$\4\X46:Give double-definition error and change $p$ to type
$t$\X≡$\6
\C{now $\\p≠\\{name_ptr}$ and $\\t≠\\{normal}$}\6
\&{begin} \37\&{if} $\\{ilk}[\\p]=\\{normal}$ \1\&{then}\6
\&{begin} \37$\\{ex_err_print}(\.{\'!\ This\ identifier\ has\ already\
appeared\'})$;\5
\X47:Remove $p$ from secondary hash table\X;\6
\&{end}\6
\4\&{else} $\\{err_print}(\.{\'!\ This\ identifier\ was\ defined\
before\'})$;\2\6
$\\{ilk}[\\p]←\\t$;\6
\&{end}\par
\U section 44.

\M47. When we have to remove a secondary hash entry, because a \\
{normal} identifier
is changing to another \\{ilk}, the hash code $h$ and chopped
identifier have
already been computed.

\Y\P$\4\X47:Remove $p$ from secondary hash table\X≡$\6
$\\q←\\{chop_hash}[\\h]$;\6
\&{if} $\\q=\\p$ \1\&{then}\5
$\\{chop_hash}[\\h]←\\{equiv}[\\p]$\6
\4\&{else} \&{begin} \37\&{while} $\\{equiv}[\\q]≠\\p$ \1\&{do}\5
$\\q←\\{equiv}[\\q]$;\2\6
$\\{equiv}[\\q]←\\{equiv}[\\p]$;\6
\&{end}\2\par
\U section 46.

\M48. The following routine could make good use of a generalized \\
{pack} procedure
that puts items into just part of a packed array instead of the whole
thing.

\Y\P$\4\X48:Enter a new identifier into the table at position $p$\X≡$\6
\&{begin} \37\&{if} $(\\t=\\{normal})∧(\\{buffer}[\\{id_first}]≠\.{""}\.
{""})$ \1\&{then}\5
\X49:Check for ambiguity and update secondary hash\X;\2\6
\&{if} $\\{byte_ptr}+\\l>\\{max_bytes}$ \1\&{then}\5
$\\{overflow}(\.{\'byte\ memory\'})$;\2\6
\&{if} $\\{name_ptr}=\\{max_names}$ \1\&{then}\5
$\\{overflow}(\.{\'name\'})$;\2\6
$\\i←\\{id_first}$;\5
$\\k←\\{byte_ptr}$;\C{get ready to move the identifier into \\
{byte_mem}}\6
\&{while} $\\i<\\{id_loc}$ \1\&{do}\6
\&{begin} \37$\\{byte_mem}[\\k]←\\{buffer}[\\i]$;\5
$\\{incr}(\\k)$;\5
$\\{incr}(\\i)$;\6
\&{end};\2\6
$\\{byte_ptr}←\\k$;\5
$\\{incr}(\\{name_ptr})$;\5
$\\{byte_start}[\\{name_ptr}]←\\k$;\6
\&{if} $\\{buffer}[\\{id_first}]≠\.{""}\.{""}$ \1\&{then}\5
$\\{ilk}[\\p]←\\t$\6
\4\&{else} \X51:Define and output a new string of the pool\X;\2\6
\&{end}\par
\U section 44.

\M49. \P$\X49:Check for ambiguity and update secondary hash\X≡$\6
\&{begin} \37$\\q←\\{chop_hash}[\\h]$;\6
\&{while} $\\q≠0$ \1\&{do}\6
\&{begin} \37\X50:Check if $q$ conflicts with $p$\X;\6
$\\q←\\{equiv}[\\q]$;\6
\&{end};\2\6
$\\{equiv}[\\p]←\\{chop_hash}[\\h]$;\5
$\\{chop_hash}[\\h]←\\p$;\C{put $p$ at front of secondary list}\6
\&{end}\par
\U section 48.

\M50. \P$\X50:Check if $q$ conflicts with $p$\X≡$\6
\&{begin} \37$\\k←\\{byte_start}[\\q]$;\5
$\\s←0$;\6
\&{while} $(\\k<\\{byte_start}[\\q+1])∧(\\s<\\{unambig_length})$ \1\&
{do}\6
\&{begin} \37$\\c←\\{byte_mem}[\\k]$;\6
\&{if} $\\c≠\.{"_"}$ \1\&{then}\6
\&{begin} \37\&{if} $\\c≥\.{"a"}$ \1\&{then}\5
$\\c←\\c-\O40$;\C{convert to upper case}\2\6
\&{if} $\\{chopped_id}[\\s]≠\\c$ \1\&{then}\5
\&{goto} \37\\{not_found};\2\6
$\\{incr}(\\s)$;\6
\&{end};\2\6
$\\{incr}(\\k)$;\6
\&{end};\2\6
\&{if} $(\\k=\\{byte_start}[\\q+1])∧(\\{chopped_id}[\\s]≠0)$ \1\&
{then}\5
\&{goto} \37\\{not_found};\2\6
$\\{ex_print_nl}(\.{\'!\ Identifier\ conflict\ with\ \'})$;\6
\&{for} $\\k←\\{byte_start}[\\q]\mathrel{\&{to}}\\{byte_start}[\\
q+1]-1$ \1\&{do}\5
$\\{print}(\\{chr}(\\{byte_mem}[\\k]))$;\2\6
\\{error};\5
$\\q←0$;\C{only one conflict will be printed, since $\\{equiv}[0]=0$}\6
\4\\{not_found}: \37\&{end}\par
\U section 49.

\M51. \P$\X51:Define and output a new string of the pool\X≡$\6
\&{begin} \37$\\{ilk}[\\p]←\\{numeric}$;\C{strings are like numeric
macros}\6
\&{if} $\\l-\\{double_chars}=2$ \1\&{then}\C{this string is for a
single character}\6
$\\{equiv}[\\p]←\\{buffer}[\\{id_first}+1]+\O100000$\6
\4\&{else} \&{begin} \37$\\{equiv}[\\p]←\\{string_ptr}+\O100000$;\5
$\\{incr}(\\{string_ptr})$;\5
$\\{write}(\\{pool},\39\\{chr}(\O37+\\l-\\{double_chars}))$;\C{output
string length plus \O40}\6
$\\i←\\{id_first}+1$;\6
\&{while} $\\i<\\{id_loc}$ \1\&{do}\6
\&{begin} \37$\\{write}(\\{pool},\39\\{chr}(\\{buffer}[\\
i]))$;\C{output characters of string}\6
\&{if} $(\\{buffer}[\\i]=\.{""}\.{""})∨(\\{buffer}[\\i]=\.{"@"})$ \1\&
{then}\5
$\\i←\\i+2$\C{omit second appearance of doubled character}\6
\4\&{else} $\\{incr}(\\i)$;\2\6
\&{end};\2\6
\&{end};\2\6
\&{end}\par

\U section 48.
\N52.  Searching for module names.
The \\{mod_lookup} procedure finds the module name $\\{module}[1\to\\
l]$ in the
search tree, after inserting it if necessary, and returns a pointer to
where it was found. According to the rules of \.{WEB}, no module name
should be a proper prefix of another, so a ``clean'' comparison should
occur between any two names. The result of \\{mod_lookup} is↔0 if this
prefix condition is violated.

\Y\P$\4\X12:Globals in the outer block\X\mathrel{+}≡$\6
\4\\{module}: \37\&{array} $[0\to\\{longest_name}]$ \1\&{of}\5
\\{ascii_code};\C{name being sought for}\2\par

\M53. \P\6
\4\&{function}\1\  \37$\\{mod_lookup}(\\l:\\{sixteen_bits})$: \37\\
{name_pointer};\C{finds module name}\6
\4\&{label} \37\\{found};\6
\4\&{var} \37\\c: \37$(\\{less},\39\\{equal},\39\\{greater},\39\\
{prefix},\39\\{extension})$;\C{comparison between two names}\6
\\j: \37$0\to\\{longest_name}$;\C{index into \\{module}}\6
\\k: \37$0\to\\{max_bytes}$;\C{index into \\{byte_mem}}\6
\\p: \37\\{name_pointer};\C{current node of the search tree}\6
\\q: \37\\{name_pointer};\C{father of node $p$}\2\6
\&{begin} \37$\\c←\\{greater}$;\5
$\\q←0$;\5
$\\p←\\{rlink}[0]$;\C{$\\{rlink}[0]$ is the root of the tree}\6
\&{while} $\\p≠0$ \1\&{do}\6
\&{begin} \37\X55:Set $c$ to the result of comparing given name to name
$p$\X;\6
$\\q←\\p$;\6
\&{if} $\\c=\\{less}$ \1\&{then}\5
$\\p←\\{llink}[\\q]$\6
\4\&{else} \&{if} $\\c=\\{greater}$ \1\&{then}\5
$\\p←\\{rlink}[\\q]$\6
\4\&{else} \&{goto} \37\\{found};\2\2\6
\&{end};\2\6
\X54:Enter new module name into the tree\X;\6
\4\\{found}: \37\&{if} $\\c≠\\{equal}$ \1\&{then}\6
\&{begin} \37$\\{ex_err_print}(\.{\'!\ Incompatible\ module\ names\'
})$;\5
$\\p←0$;\6
\&{end};\2\6
$\\{mod_lookup}←\\p$;\6
\&{end};\par

\M54. \P$\X54:Enter new module name into the tree\X≡$\6
\&{if} $\\{byte_ptr}+\\l>\\{max_bytes}$ \1\&{then}\5
$\\{overflow}(\.{\'byte\ memory\'})$;\2\6
\&{if} $\\{name_ptr}=\\{max_names}$ \1\&{then}\5
$\\{overflow}(\.{\'name\'})$;\2\6
$\\p←\\{name_ptr}$;\6
\&{if} $\\c=\\{less}$ \1\&{then}\5
$\\{llink}[\\q]←\\p$\6
\4\&{else} $\\{rlink}[\\q]←\\p$;\2\6
$\\{llink}[\\p]←0$;\5
$\\{rlink}[\\p]←0$;\5
$\\c←\\{equal}$;\6
\&{for} $\\j←1\mathrel{\&{to}}\\l$ \1\&{do}\5
$\\{byte_mem}[\\{byte_ptr}+\\j-1]←\\{module}[\\j]$;\2\6
$\\{byte_ptr}←\\{byte_ptr}+\\l$;\5
$\\{incr}(\\{name_ptr})$;\5
$\\{byte_start}[\\{name_ptr}]←\\{byte_ptr}$\par
\U section 53.

\M55. \P$\X55:Set $c$ to the result of comparing given name to name
$p$\X≡$\6
$\\k←\\{byte_start}[\\p]$;\5
$\\c←\\{equal}$;\5
$\\j←1$;\6
\&{while} $(\\k<\\{byte_start}[\\p+1])∧(\\j≤\\l)∧(\\{module}[\\j]=\\
{byte_mem}[\\k])$ \1\&{do}\6
\&{begin} \37$\\{incr}(\\k)$;\5
$\\{incr}(\\j)$;\6
\&{end};\2\6
\&{if} $\\k=\\{byte_start}[\\p+1]$ \1\&{then}\6
\&{if} $\\j>\\l$ \1\&{then}\5
$\\c←\\{equal}$\6
\4\&{else} $\\c←\\{extension}$\2\6
\4\&{else} \&{if} $\\j>\\l$ \1\&{then}\5
$\\c←\\{prefix}$\6
\4\&{else} \&{if} $\\{module}[\\j]<\\{byte_mem}[\\k]$ \1\&{then}\5
$\\c←\\{less}$\6
\4\&{else} $\\c←\\{greater}$\2\2\2\par
\U sections 53 and 56.

\M56. The \\{prefix_lookup} procedure is supposed to find exactly one
module
name that has $\\{module}[1\to\\l]$ as a prefix. Actually the algorithm
silently
accepts also the situation that some module name is a prefix of $\\
{module}[1\to\\l]$,
because the user is unlikely to object.

\Y\P\4\&{function}\1\  \37$\\{prefix_lookup}(\\l:\\{sixteen_bits})$: \3
7\\{name_pointer};\C{finds name extension}\6
\4\&{label} \37\\{found};\6
\4\&{var} \37\\c: \37$(\\{less},\39\\{equal},\39\\{greater},\39\\
{prefix},\39\\{extension})$;\C{comparison between two names}\6
\\{count}: \37$0\to\\{max_names}$;\C{the number of hits}\6
\\j: \37$0\to\\{longest_name}$;\C{index into \\{module}}\6
\\k: \37$0\to\\{max_bytes}$;\C{index into \\{byte_mem}}\6
\\p: \37\\{name_pointer};\C{current node of the search tree}\6
\\q: \37\\{name_pointer};\C{another place to resume the search after
one branch is done}\6
\\r: \37\\{name_pointer};\C{extension found}\2\6
\&{begin} \37$\\q←0$;\5
$\\p←\\{rlink}[0]$;\5
$\\{count}←0$;\5
$\\r←0$;\C{begin search at root of tree}\6
\&{while} $\\p≠0$ \1\&{do}\6
\&{begin} \37\X55:Set $c$ to the result of comparing given name to name
$p$\X;\6
\&{if} $\\c=\\{less}$ \1\&{then}\5
$\\p←\\{llink}[\\p]$\6
\4\&{else} \&{if} $\\c=\\{greater}$ \1\&{then}\5
$\\p←\\{rlink}[\\p]$\6
\4\&{else} \&{begin} \37$\\r←\\p$;\5
$\\{incr}(\\{count})$;\5
$\\q←\\{rlink}[\\p]$;\5
$\\p←\\{llink}[\\p]$;\6
\&{end};\2\2\6
\&{if} $\\p=0$ \1\&{then}\6
\&{begin} \37$\\p←\\q$;\5
$\\q←0$;\6
\&{end};\2\6
\&{end};\2\6
\&{if} $\\{count}≠1$ \1\&{then}\6
\&{if} $\\{count}=0$ \1\&{then}\5
$\\{err_print}(\.{\'!\ Name\ does\ not\ match\'})$\6
\4\&{else} $\\{err_print}(\.{\'!\ Ambiguous\ prefix\'})$;\2\2\6
$\\{prefix_lookup}←\\r$;\C{the result will be 0 if there was no match}\6
\&{end};\par
\N57.  Tokens.
Replacement texts, which represent \PASCAL\ code in a compressed
format,
appear in \\{tok_mem} as mentioned above. The codes in
these texts are called `tokens'; some tokens occupy two consecutive
eight-bit byte positions, and the others take just one byte.

If $p>0$ points to a replacement text, $\\{tok_start}[\\p]$ is the \\
{tok_mem} position
of the first eight-bit code of that text. If $\\{text_link}[\\p]=0$,
this is the replacement text for a macro, otherwise it is the
replacement
text for a module. In the latter case $\\{text_link}[\\p]$ is either
equal to
\\{module_flag}, which means that there is no further text for this
module, or
$\\{text_link}[\\p]$ points to a
continuation of this replacement text; such links are created when
several modules have \PASCAL\ texts with the same name, and they also
tie together all the \PASCAL\ texts of unnamed modules.
The replacement text pointer for the first unnamed module
appears in $\\{text_link}[0]$, and the most recent such pointer is \\
{last_unnamed}.

\Y\P\D \37$\\{module_flag}≡\\{max_texts}$\C{final \\{link} in module
replacement texts}\par
\Y\P$\4\X12:Globals in the outer block\X\mathrel{+}≡$\6
\4\\{last_unnamed}: \37\\{text_pointer};\C{most recent replacement text
of unnamed module}\par

\M58. \P$\X13:Set initial values\X\mathrel{+}≡$\6
$\\{last_unnamed}←0$;\5
$\\{text_link}[0]←0$;\par

\M59. If the first byte of a token is less than 200, the token occupies
a
single byte. Otherwise we make a sixteen-bit token by combining two
consecutive
bytes $a$ and $b$. If $200≤a<250$, then $(a-200)\times2↑8+b$ points
to an identifier; if $250≤a<320$, then
$(a-250)\times2↑8+b$ points to a module name; otherwise, i.e., if
$320≤a<400$, then $(a-320)\times2↑8+b$ is the number of the module
in which the current replacement text appears.

Codes less than 200 are 7-bit ascii codes that represent themselves.
In particular, a single-character identifier like `\\x' will be a
one-byte
token, while all longer identifiers will occupy two bytes.

Some of the 7-bit ascii codes will not be present, however, so we can
use them for special purposes. The following symbolic names are used:

\yskip\hang \\{begin_comment} denotes \.{@\{}, which will become either
\.{\{} or \.{[}.

\yskip\hang \\{end_comment} denotes \.{@\}}, which will become either
\.{\}} or \.{]}.

\yskip\hang \\{octal} denotes the \O that precedes an octal constant.

\yskip\hang \\{param} denotes insertion of a parameter. This occurs
only in
the replacement texts of parametric macros, outside of single-quoted
strings
in those texts.

\yskip\hang \\{join} denotes the concatenation of adjacent items with
no
space or line breaks allowed between them (the $@&$ operation of \.
{WEB}).

\yskip\hang \\{double_dot} denotes `\.{..}' in \PASCAL.

\Y\P\D \37$\\{begin_comment}=\O11$\C{ascii tab mark will not appear}\par
\P\D \37$\\{end_comment}=\O12$\C{ascii line feed will not appear}\par
\P\D \37$\\{octal}=\O14$\C{ascii form feed will not appear}\par
\P\D \37$\\{param}=\O15$\C{ascii carriage return will not appear}\par
\P\D \37$\\{double_dot}=\O40$\C{ascii space will not appear except in
strings}\par
\P\D \37$\\{join}=\O177$\C{ascii delete will not appear}\par

\M60. The following procedure is used to enter a two-byte value into
\\{tok_mem} when a replacement text is being generated.

\Y\P\4\&{procedure}\1\  \37$\\{store_two_bytes}(\\x:\\
{sixteen_bits})$;\C{stores high byte, then low byte}\2\6
\&{begin} \37\&{if} $\\{tok_ptr}+2>\\{max_toks}$ \1\&{then}\5
$\\{overflow}(\.{\'token\'})$;\2\6
$\\{tok_mem}[\\{tok_ptr}]←\\x\mathbin{\&{div}}\O400$;\C{this could be
done by a shift command}\6
$\\{tok_mem}[\\{tok_ptr}+1]←\\x\mathbin{\&{mod}}\O400$;\C{this could be
done by a logical and}\6
$\\{tok_ptr}←\\{tok_ptr}+2$;\6
\&{end};\par

\M61. When \.{TANGLE} is being operated in debug mode, it has a
procedure to display
a replacement text in symbolic form. This procedure has not been
spruced up to
generate a real great format, but at least the results are not as bad
as
a memory dump.

\Y\P\\{DEBUG} \6
\4\&{procedure}\1\  \37$\\{print_repl}(\\p:\\{text_pointer})$;\6
\4\&{var} \37\\k: \37$0\to\\{max_toks}$;\C{index into \\{tok_mem}}\6
\\a: \37\\{sixteen_bits};\C{current byte(s)}\2\6
\&{begin} \37\&{if} $\\p≥\\{text_ptr}$ \1\&{then}\5
$\\{print}(\.{\'BAD\'})$\6
\4\&{else} \&{begin} \37$\\k←\\{tok_start}[\\p]$;\6
\&{while} $\\k<\\{tok_start}[\\p+1]$ \1\&{do}\6
\&{begin} \37$\\a←\\{tok_mem}[\\k]$;\6
\&{if} $\\a≥\O200$ \1\&{then}\5
\X62:Display two-byte token starting with $a$\X\6
\4\&{else} \X63:Display one-byte token $a$\X;\2\6
$\\{incr}(\\k)$;\6
\&{end};\2\6
\&{end};\2\6
\&{end};\5
\\{GUBED}\par

\M62. \P$\X62:Display two-byte token starting with $a$\X≡$\6
\&{begin} \37$\\{incr}(\\k)$;\6
\&{if} $\\a<\O250$ \1\&{then}\C{identifier or string}\6
\&{begin} \37$\\a←(\\a-\O200)\ast\O400+\\{tok_mem}[\\k]$;\5
$\\{print_id}(\\a)$;\6
\&{if} $\\{byte_mem}[\\{byte_start}[\\a]]=\.{""}\.{""}$ \1\&{then}\5
$\\{print}(\.{\'"\'})$\6
\4\&{else} $\\{print}(\.{\'\ \'})$;\2\6
\&{end}\6
\4\&{else} \&{if} $\\a<\O320$ \1\&{then}\C{module name}\6
\&{begin} \37$\\{print}(\.{\'@<\'})$;\5
$\\{print_id}((\\a-\O250)\ast\O400+\\{tok_mem}[\\k])$;\5
$\\{print}(\.{\'@>\'})$;\6
\&{end}\6
\4\&{else} \&{begin} \37$\\a←(\\a-\O320)\ast\O400+\\{tok_mem}[\\
k]$;\C{module number}\6
$\\{print}(\.{\'@\{\'},\39\\a:0,\39\.{\'@\'},\39\\{chr}(\.{"\}
"}))$;\C{can't use right brace in comments}\6
\&{end};\2\2\6
\&{end}\par
\U section 61.

\M63. \P$\X63:Display one-byte token $a$\X≡$\6
\&{case} $\\a$ \1\&{of}\6
\4\\{begin_comment}: \37$\\{print}(\.{\'@\{\'})$;\6
\4\\{end_comment}: \37$\\{print}(\.{\'@\'},\39\\{chr}(\.{"\}
"}))$;\C{can't use right brace in comments}\6
\4\\{octal}: \37$\\{print}(\.{\'@\'}\.{\'\'})$;\6
\4\\{param}: \37$\\{print}(\.{\'\#\'})$;\6
\4\.{"@"}: \37$\\{print}(\.{\'@@\'})$;\6
\4\&{othercases} \37$\\{print}(\\{chr}(\\a))$\2\6
\&{endcases}\par

\U section 61.
\N64.  Stacks for output.
Let's make sure that our data structures contain enough information to
produce the entire \PASCAL\ program as desired, by working next on the
algorithms that actually do produce that program.


\M65. The output process uses a stack to keep track of what is going on
at
different ``levels'' as the macros are being expanded.
Entries on this stack have four parts:

\yskip\hang \\{end_field} is the \\{tok_mem} location where the
replacement
text of a particular level will end;

\hang \\{byte_field} is the \\{tok_mem} location from which the next
token
on a particular level will be read;

\hang \\{name_field} points to the name corresponding to a particular
level;

\hang \\{repl_field} points to the replacement text currently being
read
at a particular level. (The \\{name_field} is not sufficient by itself,
because
replacement texts can be chained together in their \\{text_link}
fields.)

\yskip\noindent The current values of these four quantities are
referred to
quite frequently, so they are stored in a separate place instead of in
the \\{stack} array. We call the current values \\{cur_end}, \\
{cur_byte},
\\{cur_name}, and \\{cur_repl}.

The global variable \\{stack_ptr} tells how many levels of output are
currently in progress. The end of all output occurs when the stack is
empty, i.e., when $\\{stack_ptr}=0$.

\Y\P$\4\X11:Types in the outer block\X\mathrel{+}≡$\6
$\\{output_state}=$ \1\&{record} \37\\{end_field}: \37\\
{sixteen_bits};\C{ending location of replacement text}\6
\4\\{byte_field}: \37\\{sixteen_bits};\C{present location within
replacement text}\6
\4\\{name_field}: \37\\{name_pointer};\C{\\{byte_start} index for text
being output}\6
\4\\{repl_field}: \37\\{text_pointer};\C{\\{tok_start} index for text
being output}\2\6
\&{end};\par

\M66. \P\D \37$\\{cur_end}≡\\{cur_state}.\\{end_field}$\C{current
ending location in \\{tok_mem}}\par
\P\D \37$\\{cur_byte}≡\\{cur_state}.\\{byte_field}$\C{location of next
output byte in \\{tok_mem}}\par
\P\D \37$\\{cur_name}≡\\{cur_state}.\\{name_field}$\C{pointer to
current name being expanded}\par
\P\D \37$\\{cur_repl}≡\\{cur_state}.\\{repl_field}$\C{pointer to
current replacement text}\par
\Y\P$\4\X12:Globals in the outer block\X\mathrel{+}≡$\6
\4\\{cur_state}: \37\\{output_state};\C{\\{cur_end}, \\{cur_byte}, \\
{cur_name}, \\{cur_repl}}\6
\4\\{stack}: \37\&{array} $[1\to\\{stack_size}]$ \1\&{of}\5
\\{output_state};\C{info for non-current levels}\2\6
\4\\{stack_ptr}: \37$0\to\\{stack_size}$;\C{first unused location in
the output state stack}\par

\M67. Parameters must also be stacked. They are placed in
\\{tok_mem} just above the other replacement texts, and dummy parameter
`names' are placed in \\{byte_start} just after the other names.
The variables \\{text_ptr} and \\{tok_ptr} essentially serve as
parameter
stack pointers during the output phase, so there is no need for a
separate
data structure to handle this problem.


\M68. There is an implicit stack corresponding to meta-comments that
are output
via \.{@\{} and \.{@\}}. But this stack need not be represented in
detail,
because we only need to know whether it is empty or not. A global
variable
\\{brace_level} tells how many items would be on this stack if it were
present.

\Y\P$\4\X12:Globals in the outer block\X\mathrel{+}≡$\6
\4\\{brace_level}: \37\\{eight_bits};\C{current depth of $\.{@\{
}\ldotsm\.{@\}}$ nesting}\par

\M69. To get the output process started, we will perform the following
initialization steps. We may assume that $\\{text_link}[0]$ is nonzero,
since it
points to the \PASCAL\ text in the first unnamed module that generates
code; if there are no such modules, there is nothing to output, and an
error message will have been generated before we do any of the
initialization.

\Y\P$\4\X69:Initialize the output stacks\X≡$\6
$\\{stack_ptr}←1$;\5
$\\{brace_level}←0$;\5
$\\{cur_name}←0$;\5
$\\{cur_repl}←\\{text_link}[0]$;\5
$\\{cur_byte}←\\{tok_start}[\\{cur_repl}]$;\5
$\\{cur_end}←\\{tok_start}[\\{cur_repl}+1]$\par
\U section 98.

\M70. When the replacement text for name $p$ is to be inserted into the
output,
the following subroutine is called to save the old level of output and
get
the new one going.

\Y\P\4\&{procedure}\1\  \37$\\{push_level}(\\p:\\
{name_pointer})$;\C{suspends the current level}\2\6
\&{begin} \37\&{if} $\\{stack_ptr}=\\{stack_size}$ \1\&{then}\5
$\\{overflow}(\.{\'stack\'})$\6
\4\&{else} \&{begin} \37$\\{stack}[\\{stack_ptr}]←\\
{cur_state}$;\C{save \\{cur_end}, \\{cur_byte}, etc.}\6
$\\{incr}(\\{stack_ptr})$;\5
$\\{cur_name}←\\p$;\5
$\\{cur_repl}←\\{equiv}[\\p]$;\5
$\\{cur_byte}←\\{tok_start}[\\{cur_repl}]$;\5
$\\{cur_end}←\\{tok_start}[\\{cur_repl}+1]$;\6
\&{end};\2\6
\&{end};\par

\M71. When we come to the end of a replacement text, the \\{pop_level}
subroutine
does the right thing: It either moves to the continuation of this
replacement
text or returns the state to the most recently stacked level. Part of
this
subroutine, which updates the parameter stack, will be given later when
study the parameter stack in more detail.

\Y\P\4\&{procedure}\1\  \37\\{pop_level};\C{do this when \\{cur_byte}
reaches \\{cur_end}}\6
\4\&{label} \37\\{exit};\2\6
\&{begin} \37\&{if} $\\{text_link}[\\{cur_repl}]=0$ \1\&{then}\C{end of
macro expansion}\6
\&{begin} \37\&{if} $\\{ilk}[\\{cur_name}]=\\{parametric}$ \1\&{then}\5
\X77:Remove a parameter from the parameter stack\X;\2\6
\&{end}\6
\4\&{else} \&{if} $\\{text_link}[\\{cur_repl}]<\\{module_flag}$ \1\&
{then}\C{link to a continuation}\6
\&{begin} \37$\\{cur_repl}←\\{text_link}[\\{cur_repl}]$;\C{we will stay
on the same level}\6
$\\{cur_byte}←\\{tok_start}[\\{cur_repl}]$;\5
$\\{cur_end}←\\{tok_start}[\\{cur_repl}+1]$;\5
\&{return};\6
\&{end};\2\2\6
$\\{decr}(\\{stack_ptr})$;\C{we will go down to the previous level}\6
\&{if} $\\{stack_ptr}>0$ \1\&{then}\5
$\\{cur_state}←\\{stack}[\\{stack_ptr}]$;\2\6
\4\\{exit}: \37\&{end};\par

\M72. The heart of the output procedure is the \\{get_output} routine,
which produces
the next token of output that is not a reference to a macro. This
procedure
handles all the stacking and unstacking that is necessary. It returns
the
value \\{number} if the next output has a numeric value (the value of a
numeric macro or string), in which case \\{cur_val} has been set to the
number in question. The procedure also returns the value \\{new_module}
if the
next output begins the replacement text of some module, in which case
\\{cur_val} is that module's number. And it returns the value \\
{identifier} if
the next output is an identifier of length two or more, in which case
\\{cur_val} points to that identifier name.

\Y\P\D \37$\\{number}=\O200$\C{code returned by \\{get_output} when
next output is numeric}\par
\P\D \37$\\{module_number}=\O201$\C{code returned by \\{get_output} for
module numbers}\par
\P\D \37$\\{identifier}=\O202$\C{code returned by \\{get_output} for
identifiers}\par
\Y\P$\4\X12:Globals in the outer block\X\mathrel{+}≡$\6
\4\\{cur_val}: \37\\{integer};\C{additional information corresponding
to output token}\par

\M73. If \\{get_output} finds that no more output remains, it returns
the value zero.

\Y\P\4\&{function}\1\  \37\\{get_output}: \37\\
{sixteen_bits};\C{returns next token after macro expansion}\6
\4\&{label} \37$\\{restart},\39\\{done}$;\6
\4\&{var} \37\\a: \37\\{sixteen_bits};\C{value of current byte}\6
\\b: \37\\{eight_bits};\C{byte being copied}\6
\\{bal}: \37\\{sixteen_bits};\C{excess of \.( versus \.) while copying
a parameter}\6
\&{begin} \37\\{restart}: \37\&{if} $\\{stack_ptr}=0$ \1\&{then}\5
$\\a←0$\6
\4\&{else} \&{begin} \37\&{if} $\\{cur_byte}=\\{cur_end}$ \1\&{then}\6
\&{begin} \37\\{pop_level};\5
\&{goto} \37\\{restart};\6
\&{end};\2\6
$\\a←\\{tok_mem}[\\{cur_byte}]$;\5
$\\{incr}(\\{cur_byte})$;\6
\&{if} $\\a<\O200$ \1\&{then}\C{one-byte token}\6
\&{begin} \37\&{if} $\\a=\\{param}$ \1\&{then}\5
\X78:Start scanning current macro parameter, \&{goto} \\{restart}\X;\2\6
\&{end}\6
\4\&{else} \&{begin} \37$\\a←(\\a-\O200)\ast\O400+\\{tok_mem}[\\
{cur_byte}]$;\5
$\\{incr}(\\{cur_byte})$;\6
\&{if} $\\a<\O24000$ \1\&{then}\C{$\O24000=(\O250-\O200)\ast\O400$}\6
\X75:Expand macro $a$, \&{goto} \\{restart} if no output found\X\6
\4\&{else} \&{if} $\\a<\O50000$ \1\&
{then}\C{$\O50000=(\O320-\O200)\ast\O400$}\6
\X74:Expand module $\\a-\O24000,$ \&{goto} \\{restart}\X\6
\4\&{else} \&{begin} \37$\\{cur_val}←\\a-\O50000$;\5
$\\a←\\{module_number}$;\6
\&{end};\2\2\6
\&{end};\2\6
\&{end};\2\6
\\{DEBUG} \6
\&{if} $\\{trouble_shooting}$ \1\&{then}\5
\\{debug_help};\2\6
$\\{GUBED}\\{get_output}←\\a$; \6
\&{end} ;\par

\M74. The user may have forgotten to give any \PASCAL\ text for a
module name,
or the \PASCAL\ text may have been associated with a different name by
mistake.

\Y\P$\4\X74:Expand module $\\a-\O24000,$ \&{goto} \\{restart}\X≡$\6
\&{begin} \37$\\a←\\a-\O24000$;\6
\&{if} $\\{equiv}[\\a]≠0$ \1\&{then}\5
$\\{push_level}(\\a)$\6
\4\&{else} \&{if} $\\a≠0$ \1\&{then}\6
\&{begin} \37$\\{ex_print_nl}(\.{\'!\ Not\ present:\ <\'})$;\5
$\\{print_id}(\\a)$;\5
$\\{print}(\.{\'>\'})$;\5
\\{error};\6
\&{end};\2\2\6
\&{goto} \37\\{restart};\6
\&{end}\par
\U section 73.

\M75. \P$\X75:Expand macro $a$, \&{goto} \\{restart} if no output
found\X≡$\6
\&{begin} \37\&{case} $\\{ilk}[\\a]$ \1\&{of}\6
\4\\{normal}: \37\&{begin} \37$\\{cur_val}←\\a$;\5
$\\a←\\{identifier}$;\6
\&{end};\6
\4\\{numeric}: \37\&{begin} \37$\\{cur_val}←\\{equiv}[\\a]-\O100000$;\5
$\\a←\\{number}$;\6
\&{end};\6
\4\\{simple}: \37\&{begin} \37$\\{push_level}(\\a)$;\5
\&{goto} \37\\{restart};\6
\&{end};\6
\4\\{parametric}: \37\&{begin} \37\X76:Put a parameter on the parameter
stack, or \&{goto} \\{restart} if error occurs\X;\6
$\\{push_level}(\\a)$;\5
\&{goto} \37\\{restart};\6
\&{end};\6
\4\&{othercases} \37$\\{confusion}(\.{\'output\'})$\2\6
\&{endcases}\6
\&{end}\par
\U section 73.

\M76. We come now to the interesting part, the job of putting a
parameter on
the parameter stack. First we pop the stack if necessary until getting
to
a level that hasn't ended. Then the next character must be a `\.(';
and since parentheses are balanced on each level, the entire parameter
must
be present, so we can copy it without difficulty.

\Y\P$\4\X76:Put a parameter on the parameter stack, or \&{goto} \\
{restart} if error occurs\X≡$\6
\&{while} $(\\{cur_byte}=\\{cur_end})∧(\\{stack_ptr}>0)$ \1\&{do}\5
\\{pop_level};\2\6
\&{if} $(\\{stack_ptr}=0)∨(\\{tok_mem}[\\{cur_byte}]≠\.{"("})$ \1\&
{then}\6
\&{begin} \37$\\{ex_print_nl}(\.{\'!\ No\ parameter\ given\ for\ \'
})$;\5
$\\{print_id}(\\a)$;\5
\\{error};\5
\&{goto} \37\\{restart};\6
\&{end}\X79:Copy the parameter into \\{tok_mem}\X;\2\6
$\\{equiv}[\\{name_ptr}]←\\{text_ptr}$;\5
$\\{ilk}[\\{name_ptr}]←\\{simple}$; \\{DEBUG} \6
\&{if} $\\{byte_ptr}=\\{max_bytes}$ \1\&{then}\5
$\\{overflow}(\.{\'byte\ memory\'})$;\2\6
$\\{byte_mem}[\\{byte_ptr}]←\.{"\#"}$;\5
$\\{incr}(\\{byte_ptr})$;\5
\\{GUBED}\C{this is the parameter identifier for debugging printouts}\6
\&{if} $\\{name_ptr}=\\{max_names}$ \1\&{then}\5
$\\{overflow}(\.{\'name\'})$;\2\6
$\\{incr}(\\{name_ptr})$;\5
$\\{byte_start}[\\{name_ptr}]←\\{byte_ptr}$;\6
\&{if} $\\{text_ptr}=\\{max_texts}$ \1\&{then}\5
$\\{overflow}(\.{\'text\'})$;\2\6
$\\{text_link}[\\{text_ptr}]←0$;\5
$\\{incr}(\\{text_ptr})$;\5
$\\{tok_start}[\\{text_ptr}]←\\{tok_ptr}$\par
\U section 75.

\M77. The \\{pop_level} routine undoes the effect of parameter-pushing
when
a parameter macro is finished:

\Y\P$\4\X77:Remove a parameter from the parameter stack\X≡$\6
\&{begin}  $\\{STAT}\\{tok_ptr}>\\{max_tok_ptr}$ \&{then} $\\
{max_tok_ptr}←\\{tok_ptr}$;\5
\\{TATS}\C{the maximum value of \\{tok_ptr} occurs just before
parameter popping}\6
$\\{decr}(\\{name_ptr})$;\5
$\\{decr}(\\{text_ptr})$;\5
$\\{tok_ptr}←\\{tok_start}[\\{text_ptr}]$;\5
$\\{DEBUG}\\{decr}(\\{byte_ptr})$;\5
\\{GUBED} \6
\&{end} \par
\U section 71.

\M78. When a parameter occurs in a replacement text, we treat it as a
simple
macro in position ($\\{name_ptr}-1$):

\Y\P$\4\X78:Start scanning current macro parameter, \&{goto} \\
{restart}\X≡$\6
\&{begin} \37$\\{push_level}(\\{name_ptr}-1)$;\5
\&{goto} \37\\{restart};\6
\&{end}\par
\U section 73.

\M79. Similarly, a \\{param} token encountered as we copy a parameter
is converted
into a simple macro call for $\\{name_ptr}-1$.
Some care is needed to handle cases like $\\{macro}(\#;\,\\{print}(\.{\'
\#)\'}))$; the \.{\#}
token will have been changed to \\{param} outside of strings, but we
still
must distinguish `real' parentheses from those in strings.
Note: The \\{app_repl} macro is not enclosed with  \&{begin}  and   \&
{end} . Use with care.

\Y\P\D $\\{app_repl}(\#)≡$ \6
\&{if} $\\{tok_ptr}=\\{max_toks}$ \1\&{then}\5
$\\{overflow}(\.{\'token\'})$;\2\6
$\\{tok_mem}[\\{tok_ptr}]←\#$;\5
$\\{incr}(\\{tok_ptr})$\par
\Y\P$\4\X79:Copy the parameter into \\{tok_mem}\X≡$\6
$\\{bal}←1$;\5
$\\{incr}(\\{cur_byte})$;\C{skip the opening `\.('}\6
\&{loop}\1\6
\&{begin} \37$\\b←\\{tok_mem}[\\{cur_byte}]$;\5
$\\{incr}(\\{cur_byte})$;\6
\&{if} $\\b=\\{param}$ \1\&{then}\5
$\\{store_two_bytes}(\\{name_ptr}+\O77777)$\6
\4\&{else} \&{begin} \37\&{if} $\\b≥\O200$ \1\&{then}\6
\&{begin} \37$\\{app_repl}(\\b)$;\5
$\\b←\\{tok_mem}[\\{cur_byte}]$;\5
$\\{incr}(\\{cur_byte})$;\6
\&{end}\6
\4\&{else} \&{case} $\\b$ \1\&{of}\6
\4\.{"("}: \37$\\{incr}(\\{bal})$;\6
\4\.{")"}: \37\&{begin} \37$\\{decr}(\\{bal})$;\6
\&{if} $\\{bal}=0$ \1\&{then}\5
\&{goto} \37\\{done};\2\6
\&{end};\6
\4\.{"\'"}: \37\1\&{repeat} \37$\\{app_repl}(\\b)$;\5
$\\b←\\{tok_mem}[\\{cur_byte}]$;\5
$\\{incr}(\\{cur_byte})$;\6
\4\&{until}\5
$\\b=\.{"\'"}$;\C{copy string, don't change \\{bal}}\2\6
\4\&{othercases} \37\\{do_nothing}\2\6
\&{endcases};\2\6
$\\{app_repl}(\\b)$;\6
\&{end};\2\6
\&{end};\2\6
\4\\{done}: \37\par

\U section 76.
\N80.  Producing the output.
The \\{get_output} routine above handles most of the complexity of
output
generation, but there are two further considerations that have a
nontrivial
effect on \.{TANGLE}'s algorithms.

First, we want to make sure that the output is broken into lines not
exceeding \\{line_length} characters per line, where these breaks occur
at
valid places (e.g., not in the middle of a string or a constant or an
identifier, not between `\.<' and `\.>', not at a `\.{@&}' position
where quantities are being joined together. Therefore we assemble the
output into a buffer before deciding where the line breaks will appear.
However, we make no attempt to make ``logical'' line breaks that would
enhance
the readability of the output; people are supposed to read the input of
\.{TANGLE} or the \TEX ed output of \.{WEAVE} but not the tangled-up
output.

Second, we want to decimalize octal constants, and to combine integer
quantities that are added or subtracted, because \PASCAL\ doesn't allow
constant expressions in subrange types or in case labels. This means we
want to have a procedure that treats a construction like \.{(E-15+y)}
as equivalent to `\.{(E+2)}', while also leaving `\.{(1E-15+y)}' and
`\.{(E-15+17*y)}' untouched. Consider also `\.{-15+17.5}' versus
`\.{-15+17..5}'. We shall not combine integers preceding or following
\.*, \./, \.{div}, \.{mod}, or \.{@&}. Note that if $y$ has been
defined
to equal $-2$, we must expand `\.{x*y}' into `\.{x*(-2)}'; but `\.
{x-y}'
can expand into `\.{x+2}' and we can even change `\.{x-y mod z}' to
`\.{x+2 mod z}' because \PASCAL\ has a nonstandard \&{mod} operation!

The following solution to these problems has been adopted: An array
\\{out_buf} contains characters that have been generated but not yet
output,
and there are two pointers into this array. One of these, \\{out_ptr},
is
the number of characters currently in the buffer, and we will have
$1≤\\{out_ptr}≤\\{line_length}$ most of the time. The other is \\
{break_ptr},
which is the largest value $≤\\{out_ptr}$ such that we are definitely
entitled
to end a line by outputting the characters $\\{out_buf}[1\to(\\
{break_ptr}-1)]$;
we will always have $\\{break_ptr}≤\\{line_length}$.

\Y\P$\4\X12:Globals in the outer block\X\mathrel{+}≡$\6
\4\\{out_buf}: \37\&{array} $[0\to\\{out_buf_size}]$ \1\&{of}\5
\\{ascii_code};\C{assembled characters}\2\6
\4\\{out_ptr}: \37$0\to\\{out_buf_size}$;\C{first available place in \\
{out_buf}}\6
\4\\{break_ptr}: \37$0\to\\{out_buf_size}$;\C{last breaking place in \\
{out_buf}}\par

\M81. Besides these two pointers, the output process is in one of
several states:

\yskip\hang \\{num_or_id} means that the last item in the buffer is a
number or
identifier, hence a blank space or line break must be inserted if the
next
item is also a number or identifier.

\yskip\hang \\{unbreakable} means that the last item in the buffer was
followed
by the \.{@&} operation that inhibits spaces between it and the next
item.

\yskip\hang \\{sign} means that the last item in the buffer is to be
followed
by \.+ or \.-, depending on whether \\{out_app} is positive or
negative.

\yskip\hang \\{sign_val} means that the decimal equivalent of
$\leftv\\{out_val}\rightv$ should be appended to the buffer; if
$out_val<0$,
it should be preceded by a minus sign, otherwise it should be preceded
by
the character \\{out_sign} unless $\\{out_sign}=0$.

\yskip\hang \\{sign_val_sign} is like \\{sign_val}, but also append \.+
or \.-
afterwards, depending on whether \\{out_app} is positive or negative.

\yskip\hang \\{sign_val_val} is like \\{sign_val}, but also append the
decimal
equivalent of \\{out_app} including its sign.

\yskip\hang \\{misc} means none of the above.

\yskip\noindent
For example, the output buffer and output state run through the
following
sequence as we generate characters from `\.{(x-15+19-2)}':
$$\vbox{\halign{#\hfil⊗\quad\hfil#\hfil⊗\quad\hfil#\hfil⊗\quad
\hfil#\hfil⊗\quad\hfil#\hfil\cr
\\{out_buf}⊗\\{out_state}⊗\\{out_sign}⊗\\{out_val}⊗\\{out_app}\cr
\noalign{\vskip 3pt}
\.(⊗\\{misc}\cr
\.{(x}⊗\\{num_or_id}\cr
\.{(x}⊗\\{sign}⊗⊗⊗$-1$\cr
\.{(x}⊗\\{sign_val}⊗\.{"+"}⊗$-15$\cr
\.{(x}⊗\\{sign_val_sign}⊗\.{"+"}⊗$-15$⊗$+1$\cr
\.{(x}⊗\\{sign_val_val}⊗\.{"+"}⊗$-15$⊗$+19$\cr
\.{(x}⊗\\{sign_val_sign}⊗\.{"+"}⊗$+4$⊗$-1$\cr
\.{(x}⊗\\{sign_val_val}⊗\.{"+"}⊗$+4$⊗$-2$\cr
\.{(x+2)}⊗\\{misc}\cr}}$$
At each stage we have put as much into the buffer as possible without
knowing what is coming next.

In states \\{num_or_id}, \\{unbreakable}, and \\{misc} the last item in
the buffer
lies between \\{break_ptr} and $\\{out_ptr}-1$, inclusive; in the other
states we
have $\\{break_ptr}=\\{out_ptr}$.

The numeric values assigned to \\{num_or_id}, etc., have been chosen to
shorten some of the program logic; for example, the program makes use
of
the fact that $\\{sign}+2=\\{sign_val_sign}$.

\Y\P\D \37$\\{misc}=0$\C{state associated with special characters}\par
\P\D \37$\\{num_or_id}=1$\C{state associated with numbers and
identifiers}\par
\P\D \37$\\{sign}=2$\C{state associated with pending \.+ or \.-}\par
\P\D \37$\\{sign_val}=\\{num_or_id}+2$\C{state associated with pending
sign and value}\par
\P\D \37$\\{sign_val_sign}=\\{sign}+2$\C{\\{sign_val} followed by
another pending sign}\par
\P\D \37$\\{sign_val_val}=\\{sign_val}+2$\C{\\{sign_val} followed by
another pending value}\par
\P\D \37$\\{unbreakable}=\\{sign_val_val}+1$\C{state associated with \.
{@&}}\par
\Y\P$\4\X12:Globals in the outer block\X\mathrel{+}≡$\6
\4\\{out_state}: \37\\{eight_bits};\C{current status of partial
output}\6
\4$\\{out_val},\39\\{out_app}$: \37\\{integer};\C{pending values}\6
\4\\{out_sign}: \37\\{ascii_code};\C{sign to use if appending $\\
{out_val}≥0$}\par

\M82. During the output process, \\{line} will equal the number of the
next line
to be output.

\Y\P$\4\X82:Initialize the output buffer\X≡$\6
$\\{out_state}←\\{misc}$;\5
$\\{out_ptr}←0$;\5
$\\{break_ptr}←0$;\5
$\\{out_buf}[0]←0$;\5
$\\{line}←1$\par
\U section 98.

\M83. Here is a simple routine that is invoked when $\\{out_ptr}>\\
{line_length}$
or when it is time to flush out the final line. The \\{flush_buffer}
procedure
writes out the line up to the current \\{break_ptr} position, then
moves the
remaining information to the front of \\{out_buf}.

\Y\P\D $\\{check_break}≡$ \6
\&{if} $\\{out_ptr}>\\{line_length}$ \1\&{then}\5
\\{flush_buffer}\2\par
\Y\P\4\&{procedure}\1\  \37\\{flush_buffer};\C{writes one line to
output file}\6
\4\&{var} \37\\k: \37$0\to\\{out_buf_size}$;\C{index into \\{out_buf}}\2
\6
\&{begin} \37\&{for} $\\k←1\mathrel{\&{to}}\\{break_ptr}$ \1\&{do}\5
$\\{write}(\\{chr}(\\{out_buf}[\\k-1]))$;\2\6
\\{write_ln};\5
$\\{incr}(\\{line})$;\6
\&{if} $\\{line}\mathbin{\&{mod}}100=0$ \1\&{then}\5
$\\{print}(\.{\'.\'})$;\2\6
\&{if} $\\{break_ptr}<\\{out_ptr}$ \1\&{then}\6
\&{begin} \37\&{if} $\\{out_buf}[\\{break_ptr}]=\.{"\ "}$ \1\&{then}\5
$\\{incr}(\\{break_ptr})$;\C{drop space at break}\2\6
\&{for} $\\k←\\{break_ptr}\mathrel{\&{to}}\\{out_ptr}-1$ \1\&{do}\5
$\\{out_buf}[\\k-\\{break_ptr}]←\\{out_buf}[\\k]$;\2\6
\&{end};\2\6
$\\{out_ptr}←\\{out_ptr}-\\{break_ptr}$;\5
$\\{break_ptr}←0$;\6
\&{if} $\\{out_ptr}>\\{line_length}$ \1\&{then}\6
\&{begin} \37$\\{ex_err_print}(\.{\'!\ Long\ line\ must\ be\ truncated\'
})$;\5
$\\{out_ptr}←\\{line_length}$;\6
\&{end};\2\6
\&{end};\par

\M84. \P$\X84:Empty the last line from the buffer\X≡$\6
\&{if} $(\\{out_state}≠\\{misc})∨(\\{out_buf}[\\{break_ptr}]≠\.{"."})$
\1\&{then}\5
$\\{err_print}(\.{\'!\ Program\ didn\'}\.{\'t\ end\ with\ period\'})$;\2
\6
$\\{break_ptr}←\\{out_ptr}$;\5
\\{flush_buffer}\par
\U section 98.

\M85. Another simple and useful routine appends the decimal equivalent
of
a nonnegative integer to the output buffer.

\Y\P\D $\\{app}(\#)≡$ \6
\&{begin} \37$\\{out_buf}[\\{out_ptr}]←\#$;\5
$\\{incr}(\\{out_ptr})$;\C{appends a single character}\6
\&{end}\par
\P\D \37$\\{ex_app}(\#)≡\\{out_buf}[\\{out_ptr}]←\#$;\5
$\\{incr}(\\{out_ptr})$\par
\Y\P\4\&{procedure}\1\  \37$\\{app_val}(\\v:\\{integer})$;\C{puts $v$
into buffer, assumes $v≥0$}\6
\4\&{var} \37\\k: \37$0\to\\{out_buf_size}$;\C{index into \\{out_buf}}\2
\6
\&{begin} \37$\\k←\\{out_buf_size}$;\C{first we put the digits at the
very end of \\{out_buf}}\6
\1\&{repeat} \37$\\{out_buf}[\\k]←\\v\mathbin{\&{mod}}10$;\5
$\\v←\\v\mathbin{\&{div}}10$;\5
$\\{decr}(\\k)$;\6
\4\&{until}\5
$\\v=0$;\2\6
\1\&{repeat} \37$\\{incr}(\\k)$;\5
$\\{ex_app}(\\{out_buf}[\\k]+\.{"0"})$;\6
\4\&{until}\5
$\\k=\\{out_buf_size}$;\C{then we append them, most significant first}\2
\6
\&{end};\par

\M86. The output states are kept up to date by the output routines,
which are
called \\{send_out}, \\{send_val}, and \\{send_sign}. The \\{send_out}
procedure
has two parameters: $t$ tells the type of information being sent and
$v$ contains the information proper. Some information may also be
passed
in the array \\{out_contrib}.

\yskip\hang If $\\t=\\{misc}$ then $v$ is a character to be output.

\hang If $\\t=\\{str}$ then $v$ is the length of a string or something
like `\.{<>}'
in \\{out_contrib}.

\hang If $\\t=\\{ident}$ then $v$ is the length of an identifier in \\
{out_contrib}.

\hang If $\\t=\\{frac}$ then $v$ is the length of a fraction and/or
exponent in
\\{out_contrib}.

\Y\P\D \37$\\{str}=1$\C{\\{send_out} code for a string}\par
\P\D \37$\\{ident}=2$\C{\\{send_out} code for an identifier}\par
\P\D \37$\\{frac}=3$\C{\\{send_out} code for a fraction}\par
\Y\P$\4\X12:Globals in the outer block\X\mathrel{+}≡$\6
\4\\{out_contrib}: \37\&{array} $[1\to\\{line_length}]$ \1\&{of}\5
\\{ascii_code};\C{a contribution to \\{out_buf}}\2\par

\M87. \P\6
\4\&{procedure}\1\  \37$\\{send_out}(\\t:\\{eight_bits};\,\35\\v:\\
{sixteen_bits})$;\C{outputs $v$ of type $t$}\6
\4\&{label} \37\\{restart};\6
\4\&{var} \37\\k: \37$0\to\\{line_length}$;\C{index into out_contrib}\2
\6
\&{begin} \37\X88:Get the buffer ready for appending the new
information\X;\6
\&{if} $\\t≠\\{misc}$ \1\&{then}\6
\&{for} $\\k←1\mathrel{\&{to}}\\v$ \1\&{do}\5
$\\{app}(\\{out_contrib}[\\k])$\2\6
\4\&{else} $\\{app}(\\v)$;\2\6
\\{check_break};\6
\&{if} $\\t≥\\{ident}$ \1\&{then}\5
$\\{out_state}←\\{num_or_id}$\C{$\\t=\\{ident}$ or \\{frac}}\6
\4\&{else} $\\{out_state}←\\{misc}$\C{$\\t=\\{str}$ or \\{misc}}\2\6
\&{end};\par

\M88. Here is where the buffer states for signs and values collapse
into simpler
states, because we are about to append something that doesn't combine
with
the previous integer constants.

We use an ascii-code trick: Since $\.{","}-1=\.{"+"}$ and $\.{","}+1=\.
{"-"}$, we have
$\.{","}-\\c=\hbox{ sign of $c$}$, when $\leftv c\rightv=1$.

\Y\P$\4\X88:Get the buffer ready for appending the new information\X≡$\6
\4\\{restart}: \37\&{case} $\\{out_state}$ \1\&{of}\6
\4\\{num_or_id}: \37\&{if} $\\t≠\\{frac}$ \1\&{then}\6
\&{begin} \37$\\{break_ptr}←\\{out_ptr}$;\6
\&{if} $\\t=\\{ident}$ \1\&{then}\5
$\\{app}(\.{"\ "})$;\2\6
\&{end};\2\6
\4\\{sign}: \37\&{begin} \37$\\{ex_app}(\.{","}-\\{out_app})$;\5
\\{check_break};\5
$\\{break_ptr}←\\{out_ptr}$;\6
\&{end};\6
\4$\\{sign_val},\39\\{sign_val_sign}$: \37\&{begin} \37\X89:Append \\
{out_val} to buffer\X;\6
$\\{out_state}←\\{out_state}-2$;\5
\&{goto} \37\\{restart};\6
\&{end};\6
\4\\{sign_val_val}: \37\X90:Reduce \\{sign_val_val} to \\{sign_val} and
\&{goto} \\{restart}\X;\6
\4\\{misc}: \37\&{if} $\\t≠\\{frac}$ \1\&{then}\5
$\\{break_ptr}←\\{out_ptr}$;\6
\4\&{othercases} \\{do_nothing}\C{this is for \\{unbreakable} state}\2\2
\6
\&{endcases}\par
\U section 87.

\M89. \P$\X89:Append \\{out_val} to buffer\X≡$\6
\&{if} $\\{out_val}<0$ \1\&{then}\5
$\\{app}(\.{"-"})$\6
\4\&{else} \&{if} $\\{out_sign}>0$ \1\&{then}\5
$\\{app}(\\{out_sign})$;\2\2\6
$\\{app_val}(\\{abs}(\\{out_val}))$;\5
\\{check_break};\par
\U sections 88 and 90.

\M90. \P$\X90:Reduce \\{sign_val_val} to \\{sign_val} and \&{goto} \\
{restart}\X≡$\6
\&{begin} \37\&{if} $(\\t=\\{frac})∨(\X91:contribution is \.* or \./ or
\.{DIV} or \.{MOD}\X)$ \1\&{then}\6
\&{begin} \37\X89:Append \\{out_val} to buffer\X;\6
$\\{out_sign}←\.{"+"}$;\5
$\\{out_val}←\\{out_app}$;\6
\&{end}\6
\4\&{else} $\\{out_val}←\\{out_val}+\\{out_app}$;\2\6
$\\{out_state}←\\{sign_val}$;\5
\&{goto} \37\\{restart};\6
\&{end}\par
\U section 88.

\M91. \P$\X91:contribution is \.* or \./ or \.{DIV} or \.{MOD}\X≡$\6
( $(\\t=\\{ident})∧(\\v=3)∧$\6
$(((\\{out_contrib}[1]=\.{"D"})∧(\\{out_contrib}[2]=\.{"I"})∧(\\
{out_contrib}[3]=\.{"V"}))∨((\\{out_contrib}[1]=\.{"M"})∧(\\
{out_contrib}[2]=\.{"O"})∧(\\{out_contrib}[3]=\.{"D"})))$ ) $∨$\6
$((\\t=\\{misc})∧((\\v=\.{"*"})∨(\\v=\.{"/"})))$\par
\U section 90.

\M92. The following routine is called with $v=\pm1$ when a plus or
minus sign is
appended to the output. It extends \PASCAL\ to allow repeated signs
(e.g., `\.{--}' is equivalent to `\.+'), rather than to give an error
message.
The signs following `\.E' in real constants are treated as part of a
fraction,
so they are not seen by this routine.

\Y\P\4\&{procedure}\1\  \37$\\{send_sign}(\\v:\\{integer})$;\2\6
\&{begin} \37\&{case} $\\{out_state}$ \1\&{of}\6
\4$\\{sign},\39\\{sign_val_sign}$: \37$\\{out_app}←\\{out_app}\ast\\
v$;\6
\4\\{sign_val}: \37\&{begin} \37$\\{out_app}←\\v$;\5
$\\{out_state}←\\{sign_val_sign}$;\6
\&{end};\6
\4\\{sign_val_val}: \37\&{begin} \37$\\{out_val}←\\{out_val}+\\
{out_app}$;\5
$\\{out_app}←\\v$;\5
$\\{out_state}←\\{sign_val_sign}$;\6
\&{end};\6
\4\&{othercases} \37\&{begin} \37$\\{break_ptr}←\\{out_ptr}$;\5
$\\{out_app}←\\v$;\5
$\\{out_state}←\\{sign}$;\6
\&{end}\2\6
\&{endcases};\6
\&{end};\par

\M93. When a (signed) integer value is to be output, we call \\
{send_val}.
Two consecutive values, although syntactically illegal, are silently
added together. In a situation like `\.{if x=y}' where $x$ has been
defined
to equal $+3$, the output will be `\.{IF+3=Y}' rather than `\.{IF
3=Y}'.

\Y\P\D \37$\\{bad_case}=666$\C{this is a label used below}\par
\Y\P\4\&{procedure}\1\  \37$\\{send_val}(\\v:\\{integer})$;\C{output
the (signed) value $v$}\6
\4\&{label} \37$\\{bad_case},\39$\C{go here if we can't keep $v$ in the
output state}\6
\\{exit};\2\6
\&{begin} \37\&{case} $\\{out_state}$ \1\&{of}\6
\4\\{num_or_id}: \37\&{begin} \37\X96:if previous output was \.{DIV} or
\.{MOD}, \&{goto} \\{bad_case}\X;\6
$\\{out_sign}←\.{"\ "}$;\5
$\\{out_state}←\\{sign_val}$;\5
$\\{out_val}←\\v$;\5
$\\{break_ptr}←\\{out_ptr}$;\6
\&{end};\6
\4\\{misc}: \37\&{begin} \37\X95:if previous output was \.* or \./, \&
{goto} \\{bad_case}\X;\6
$\\{out_sign}←0$;\5
$\\{out_state}←\\{sign_val}$;\5
$\\{out_val}←\\v$;\5
$\\{break_ptr}←\\{out_ptr}$;\6
\&{end};\6
\X94:Handle cases of \\{send_val} when \\{out_state} contains a sign\X\6
\4\&{othercases} \37\&{goto} \37\\{bad_case}\2\6
\&{endcases};\5
\&{return};\6
\4\\{bad_case}: \37\X97:Append the decimal value of $v$, with
parentheses if negative\X;\6
\4\\{exit}: \37\&{end};\par

\M94. \P$\X94:Handle cases of \\{send_val} when \\{out_state} contains
a sign\X≡$\6
\4\\{sign}: \37\&{begin} \37$\\{out_sign}←\.{"+"}$;\5
$\\{out_state}←\\{sign_val}$;\5
$\\{out_val}←\\{out_app}\ast\\v$;\6
\&{end};\6
\4\\{sign_val}: \37\&{begin} \37$\\{out_state}←\\{sign_val_val}$;\5
$\\{out_app}←\\v$;\6
\&{end};\6
\4\\{sign_val_sign}: \37\&{begin} \37$\\{out_state}←\\{sign_val_val}$;\5
$\\{out_app}←\\{out_app}\ast\\v$;\6
\&{end};\6
\4\\{sign_val_val}: \37\&{begin} \37$\\{out_val}←\\{out_val}+\\
{out_app}$;\5
$\\{out_app}←\\v$;\6
\&{end};\par
\U section 93.

\M95. \P$\X95:if previous output was \.* or \./, \&{goto} \\
{bad_case}\X≡$\6
\&{if} $(\\{out_ptr}=\\{break_ptr}+1)∧((\\{out_buf}[\\{break_ptr}]=\.
{"*"})∨(\\{out_buf}[\\{break_ptr}]=\.{"/"}))$ \1\&{then}\5
\&{goto} \37\\{bad_case}\2\par
\U section 93.

\M96. \P$\X96:if previous output was \.{DIV} or \.{MOD}, \&{goto} \\
{bad_case}\X≡$\6
\&{if} $(\\{out_ptr}=\\{break_ptr}+3)∨((\\{out_ptr}=\\{break_ptr}+4)∧(\\
{out_buf}[\\{break_ptr}]=\.{"\ "}))$ \1\&{then}  \6
\&{if} $((\\{out_buf}[\\{out_ptr}-3]=\.{"D"})∧(\\{out_buf}[\\
{out_ptr}-2]=\.{"I"})∧(\\{out_buf}[\\{out_ptr}-1]=\.{"V"}))∨$\6
$((\\{out_buf}[\\{out_ptr}-3]=\.{"M"})∧(\\{out_buf}[\\{out_ptr}-2]=\.
{"O"})∧(\\{out_buf}[\\{out_ptr}-1]=\.{"D"}))$ \&{then} \&{goto} \37\\
{bad_case}\par
\U section 93.

\M97. \P$\X97:Append the decimal value of $v$, with parentheses if
negative\X≡$\6
\&{if} $\\v≥0$ \1\&{then}\6
\&{begin} \37\&{if} $\\{out_state}=\\{num_or_id}$ \1\&{then}\6
\&{begin} \37$\\{break_ptr}←\\{out_ptr}$;\5
$\\{ex_app}(\.{"\ "})$;\6
\&{end};\2\6
$\\{app_val}(\\v)$;\5
\\{check_break};\5
$\\{out_state}←\\{num_or_id}$;\6
\&{end}\6
\4\&{else} \&{begin} \37$\\{ex_app}(\.{"("})$;\5
$\\{ex_app}(\.{"-"})$;\5
$\\{app_val}(-\\v)$;\5
$\\{ex_app}(\.{")"})$;\5
\\{check_break};\5
$\\{out_state}←\\{misc}$;\6
\&{end}\2\par

\U section 93.
\N98.  The big output switch.
To complete the output process, we need a routine that takes the
results
of \\{get_output} and feeds them to \\{send_out}, \\{send_val}, or \\
{send_sign}.
This procedure `\\{send_the_output}' will be invoked just once, as
follows:

\Y\P$\4\X98:Phase II: Output the contents of the compressed tables\X≡$\6
\&{if} $\\{text_link}[0]=0$ \1\&{then}\5
$\\{print_nl}(\.{\'!\ No\ output\ was\ specified.\'})$\6
\4\&{else} \&{begin} \37$\\{ex_print_nl}(\.{\'Writing\ the\ output\
file...\'})$;\5
\X69:Initialize the output stacks\X;\6
\X82:Initialize the output buffer\X;\6
\\{send_the_output};\5
\X84:Empty the last line from the buffer\X;\6
$\\{ex_print_nl}(\.{\'Done.\'})$;\6
\&{end}\2\par
\U section 151.

\M99. A many-way switch is used to send the output:

\Y\P\D \37$\\{get_fraction}=2$\C{this label is used below}\par
\Y\P\4\&{procedure}\1\  \37\\{send_the_output};\6
\4\&{label} \37$\\{get_fraction},\39$\C{go here to finish scanning a
real constant}\6
$\\{reswitch},\39\\{continue}$;\6
\4\&{var} \37\\{cur_char}: \37\\{eight_bits};\C{the latest character
received}\6
\\k: \37$0\to\\{line_length}$;\C{index into \\{out_contrib}}\6
\\j: \37$0\to\\{max_bytes}$;\C{index into \\{byte_mem}}\6
\\n: \37\\{integer};\C{number being scanned}\2\6
\&{begin} \37\&{while} $\\{stack_ptr}>0$ \1\&{do}\6
\&{begin} \37$\\{cur_char}←\\{get_output}$;\6
\4\\{reswitch}: \37\&{case} $\\{cur_char}$ \1\&{of}\6
\40: \37\\{do_nothing};\C{this case might arise if output ends
unexpectedly}\6
\4$\X102:Cases related to identifiers\X\X104:Cases related to
constants, possibly leading to \\{get_fraction} or \\{reswitch}\X\.
{"+"},\39\.{"-"}$: \37$\\{send_sign}(\.{","}-\\{cur_char})$;\6
\4\X100:Cases like \.{"\≠"} and \.{"←"}\X\.{"\'"}: \37\X103:Send a
string, \&{goto} \\{reswitch}\X;\6
\4\X101:Other printable characters\X: \37$\\{send_out}(\\{misc},\39\\
{cur_char})$;\6
\4\X106:Cases involving \.{@\{} and \.{@\}}\X\\{join}: \37\&{begin} \3
7$\\{send_out}(\\{frac},\390)$;\5
$\\{out_state}←\\{unbreakable}$;\6
\&{end};\6
\4\&{othercases} \37$\\{err_print}(\.{\'!\ Can\'}\.{\'t\ output\ ascii\
code\ \'},\39\\{cur_char}:0)$\2\6
\&{endcases};\5
\&{goto} \37\\{continue};\6
\4\\{get_fraction}: \37\X105:Special code to finish real constants\X;\6
\4\\{continue}: \37\&{end};\2\6
\&{end};\par

\M100. The numbers in the following definitions are ``ascii codes'' for
special
characters that aren't standard in ascii. The codes for \\{left_arrow}
and
\\{not_equal} tend to vary at different sites, but the other codes have
the stated meaning at Stanford, MIT, Carnegie, USC, etc.

\Y\P\D \37$\\{and_sign}=\O4$\par
\P\D \37$\\{not_sign}=\O5$\par
\P\D \37$\\{set_element_sign}=\O6$\par
\P\D \37$\\{or_sign}=\O37$\par
\P\D \37$\\{equivalence_sign}=\O36$\par
\P\D \37$\\{greater_or_equal}=\.{"\≥"}$\C{normally \O35}\par
\P\D \37$\\{less_or_equal}=\.{"\≤"}$\C{normally \O34}\par
\P\D \37$\\{not_equal}=\.{"\≠"}$\C{\O33 at Stanford, Carnegie, etc.;
\O32 at MIT}\par
\P\D \37$\\{left_arrow}=\.{"←"}$\C{\O137 at Stanford, Carnegie, etc.;
\O30 at MIT}\par
\Y\P$\4\X100:Cases like \.{"\≠"} and \.{"←"}\X≡$\6
\4\\{and_sign}: \37\&{begin} \37$\\{out_contrib}[1]←\.{"A"}$;\5
$\\{out_contrib}[2]←\.{"N"}$;\5
$\\{out_contrib}[3]←\.{"D"}$;\5
$\\{send_out}(\\{ident},\393)$;\6
\&{end};\6
\4\\{not_sign}: \37\&{begin} \37$\\{out_contrib}[1]←\.{"N"}$;\5
$\\{out_contrib}[2]←\.{"O"}$;\5
$\\{out_contrib}[3]←\.{"T"}$;\5
$\\{send_out}(\\{ident},\393)$;\6
\&{end};\6
\4\\{set_element_sign}: \37\&{begin} \37$\\{out_contrib}[1]←\.{"I"}$;\5
$\\{out_contrib}[2]←\.{"N"}$;\5
$\\{send_out}(\\{ident},\392)$;\6
\&{end};\6
\4\\{or_sign}: \37\&{begin} \37$\\{out_contrib}[1]←\.{"O"}$;\5
$\\{out_contrib}[2]←\.{"R"}$;\5
$\\{send_out}(\\{ident},\392)$;\6
\&{end};\6
\4\\{left_arrow}: \37\&{begin} \37$\\{out_contrib}[1]←\.{":"}$;\5
$\\{out_contrib}[2]←\.{"="}$;\5
$\\{send_out}(\\{str},\392)$;\6
\&{end};\6
\4\\{not_equal}: \37\&{begin} \37$\\{out_contrib}[1]←\.{"<"}$;\5
$\\{out_contrib}[2]←\.{">"}$;\5
$\\{send_out}(\\{str},\392)$;\6
\&{end};\6
\4\\{less_or_equal}: \37\&{begin} \37$\\{out_contrib}[1]←\.{"<"}$;\5
$\\{out_contrib}[2]←\.{"="}$;\5
$\\{send_out}(\\{str},\392)$;\6
\&{end};\6
\4\\{greater_or_equal}: \37\&{begin} \37$\\{out_contrib}[1]←\.{">"}$;\5
$\\{out_contrib}[2]←\.{"="}$;\5
$\\{send_out}(\\{str},\392)$;\6
\&{end};\6
\4\\{equivalence_sign}: \37\&{begin} \37$\\{out_contrib}[1]←\.{"="}$;\5
$\\{out_contrib}[2]←\.{"="}$;\5
$\\{send_out}(\\{str},\392)$;\6
\&{end};\6
\4\\{double_dot}: \37\&{begin} \37$\\{out_contrib}[1]←\.{"."}$;\5
$\\{out_contrib}[2]←\.{"."}$;\5
$\\{send_out}(\\{str},\392)$;\6
\&{end};\par
\U section 99.

\M101. Please don't ask how all of the following characters can
actually get
through \.{TANGLE} outside of strings. It seems that \.{""}\.{""}, \.
{"\{"},
and \.{"\}"} cannot actually occur at this point of the program, but
they have
been included just in case \.{TANGLE} changes.

\Y\P$\4\X101:Other printable characters\X≡$\6
$\.{"!"},\39\.{""}\.{""},\39\.{"\#"},\39\.{"\$"},\39\.{"\%"},\39\.
{"&"},\39\.{"("},\39\.{")"},\39\.{"*"},\39\.{","},\39\.{"/"},\39\.
{":"},\39\.{";"},\39\.{"<"},\39\.{"="},\39\.{">"},\39\.{"?"},\39\.
{"@"},\39\.{"["},\39\.{"\\"},\39\.{"]"},\39\.{"\↑"},\39\.{"_"},\39\.{"\`
"},\39\.{"\{"},\39\.{"|"},\39\.{"\}"}$\par
\U section 99.

\M102. Single-character identifiers represent themselves, while longer
ones
appear in \\{byte_mem}. All must be converted to upper case,
with underlines removed. Extremely long identifiers must be chopped.

\Y\P\D \37$\\{up_to}(\#)≡\#-24,\39\#-23,\39\#-22,\39\#-21,\39\#-20,\39\#
-19,\39\#-18,\39\#-17,\39\#-16,\39\#-15,\39\#-14,\39\#-13,\39\#-12,\39\#
-11,\39\#-10,\39\#-9,\39\#-8,\39\#-7,\39\#-6,\39\#-5,\39\#-4,\39\#-3,\3
9\#-2,\39\#-1,\39\#$\par
\Y\P$\4\X102:Cases related to identifiers\X≡$\6
\4$\.{"A"},\39\\{up_to}(\.{"Z"})$: \37\&{begin} \37$\\
{out_contrib}[1]←\\{cur_char}$;\5
$\\{send_out}(\\{ident},\391)$;\6
\&{end};\6
\4$\.{"a"},\39\\{up_to}(\.{"z"})$: \37\&{begin} \37$\\
{out_contrib}[1]←\\{cur_char}-\O40$;\5
$\\{send_out}(\\{ident},\391)$;\6
\&{end};\6
\4\\{identifier}: \37\&{begin} \37$\\k←0$;\5
$\\j←\\{byte_start}[\\{cur_val}]$;\6
\&{while} $(\\k<\\{max_id_length})∧(\\j<\\{byte_start}[\\{cur_val}+1])$
\1\&{do}\6
\&{begin} \37$\\{incr}(\\k)$;\5
$\\{out_contrib}[\\k]←\\{byte_mem}[\\j]$;\5
$\\{incr}(\\j)$;\6
\&{if} $\\{out_contrib}[\\k]≥\.{"a"}$ \1\&{then}\5
$\\{out_contrib}[\\k]←\\{out_contrib}[\\k]-\O40$\6
\4\&{else} \&{if} $\\{out_contrib}[\\k]=\.{"_"}$ \1\&{then}\5
$\\{decr}(\\k)$;\2\2\6
\&{end};\2\6
$\\{send_out}(\\{ident},\39\\k)$;\6
\&{end};\par
\U section 99.

\M103. After sending a string, we need to look ahead at the next
character, in order
to see if there were two consecutive single-quote marks. Afterwards we
go to
\\{reswitch} to process the next character.

\Y\P$\4\X103:Send a string, \&{goto} \\{reswitch}\X≡$\6
\&{begin} \37$\\k←1$;\5
$\\{out_contrib}[1]←\.{"\'"}$;\6
\1\&{repeat} \37\&{if} $\\k<\\{line_length}$ \1\&{then}\5
$\\{incr}(\\k)$;\2\6
$\\{out_contrib}[\\k]←\\{get_output}$;\6
\4\&{until}\5
$(\\{out_contrib}[\\k]=\.{"\'"})∨(\\{stack_ptr}=0)$;\2\6
\&{if} $\\k=\\{line_length}$ \1\&{then}\5
$\\{err_print}(\.{\'!\ String\ too\ long\'})$;\2\6
$\\{send_out}(\\{str},\39\\k)$;\5
$\\{cur_char}←\\{get_output}$;\6
\&{if} $\\{cur_char}=\.{"\'"}$ \1\&{then}\5
$\\{out_state}←\\{unbreakable}$;\2\6
\&{goto} \37\\{reswitch};\6
\&{end}\par
\U section 99.

\M104. \P\D \37$\\{digits}≡\.{"0"},\39\.{"1"},\39\.{"2"},\39\.{"3"},\3
9\.{"4"},\39\.{"5"},\39\.{"6"},\39\.{"7"},\39\.{"8"},\39\.{"9"}$\par
\Y\P$\4\X104:Cases related to constants, possibly leading to \\
{get_fraction} or \\{reswitch}\X≡$\6
\4\\{digits}: \37\&{begin} \37$\\n←0$;\6
\1\&{repeat} \37$\\n←10\ast\\n+\\{cur_char}-\.{"0"}$;\5
$\\{cur_char}←\\{get_output}$;\6
\4\&{until}\5
$(\\{cur_char}>\.{"9"})∨(\\{cur_char}<\.{"0"})$;\2\6
$\\{send_val}(\\n)$;\5
$\\k←0$;\6
\&{if} $\\{cur_char}=\.{"e"}$ \1\&{then}\5
$\\{cur_char}←\.{"E"}$;\2\6
\&{if} $\\{cur_char}=\.{"E"}$ \1\&{then}\5
\&{goto} \37\\{get_fraction}\6
\4\&{else} \&{goto} \37\\{reswitch};\2\6
\&{end};\6
\4\\{octal}: \37\&{begin} \37$\\n←0$;\5
$\\{cur_char}←\.{"0"}$;\6
\1\&{repeat} \37$\\n←8\ast\\n+\\{cur_char}-\.{"0"}$;\5
$\\{cur_char}←\\{get_output}$;\6
\4\&{until}\5
$(\\{cur_char}>\.{"7"})∨(\\{cur_char}<\.{"0"})$;\2\6
$\\{send_val}(\\n)$;\5
\&{goto} \37\\{reswitch};\6
\&{end};\6
\4\\{number}: \37$\\{send_val}(\\{cur_val})$;\6
\4\.{"."}: \37\&{begin} \37$\\k←1$;\5
$\\{out_contrib}[1]←\.{"."}$;\5
$\\{cur_char}←\\{get_output}$;\6
\&{if} $\\{cur_char}=\.{"."}$ \1\&{then}\6
\&{begin} \37$\\{out_contrib}[2]←\.{"."}$;\5
$\\{send_out}(\\{str},\392)$;\6
\&{end}\6
\4\&{else} \&{if} $(\\{cur_char}≥\.{"0"})∧(\\{cur_char}≤\.{"9"})$ \1\&
{then}\5
\&{goto} \37\\{get_fraction}\6
\4\&{else} \&{begin} \37$\\{send_out}(\\{misc},\39\.{"."})$;\5
\&{goto} \37\\{reswitch};\6
\&{end};\2\2\6
\&{end};\par
\U section 99.

\M105. The following code appears at label `\\{get_fraction}', when we
want to
scan to the end of a real constant. The first $k$ characters of a
fraction
have already been placed in \\{out_contrib}, and \\{cur_char} is the
next character.

\Y\P$\4\X105:Special code to finish real constants\X≡$\6
\1\&{repeat} \37\&{if} $\\k<\\{line_length}$ \1\&{then}\5
$\\{incr}(\\k)$;\2\6
$\\{out_contrib}[\\k]←\\{cur_char}$;\5
$\\{cur_char}←\\{get_output}$;\6
\&{if} $(\\{out_contrib}[\\k]=\.{"E"})∧((\\{cur_char}=\.{"+"})∨(\\
{cur_char}=\.{"-"}))$ \1\&{then}\6
\&{begin} \37\&{if} $\\k<\\{line_length}$ \1\&{then}\5
$\\{incr}(\\k)$;\2\6
$\\{out_contrib}[\\k]←\\{cur_char}$;\5
$\\{cur_char}←\\{get_output}$;\6
\&{end}\6
\4\&{else} \&{if} $\\{cur_char}=\.{"e"}$ \1\&{then}\5
$\\{cur_char}←\.{"E"}$;\2\2\6
\4\&{until}\5
$(\\{cur_char}≠\.{"E"})∧((\\{cur_char}<\.{"0"})∨(\\{cur_char}>\.
{"9"}))$;\2\6
\&{if} $\\k=\\{line_length}$ \1\&{then}\5
$\\{err_print}(\.{\'!\ Fraction\ too\ long\'})$;\2\6
$\\{send_out}(\\{frac},\39\\k)$;\5
\&{goto} \37\\{reswitch}\par
\U section 99.

\M106. \P$\X106:Cases involving \.{@\{} and \.{@\}}\X≡$\6
\4\\{begin_comment}: \37\&{begin} \37\&{if} $\\{brace_level}=0$ \1\&
{then}\5
$\\{send_out}(\\{misc},\39\.{"\{"})$\6
\4\&{else} $\\{send_out}(\\{misc},\39\.{"["})$;\2\6
$\\{incr}(\\{brace_level})$;\6
\&{end};\6
\4\\{end_comment}: \37\&{if} $\\{brace_level}>0$ \1\&{then}\6
\&{begin} \37$\\{decr}(\\{brace_level})$;\6
\&{if} $\\{brace_level}=0$ \1\&{then}\5
$\\{send_out}(\\{misc},\39\.{"\}"})$\6
\4\&{else} $\\{send_out}(\\{misc},\39\.{"]"})$;\2\6
\&{end}\6
\4\&{else} $\\{err_print}(\.{\'!\ Extra\ @\}\'})$;\2\6
\4\\{module_number}: \37\&{if} $\\{brace_level}=0$ \1\&{then}\6
\&{begin} \37$\\{send_out}(\\{misc},\39\.{"\{"})$;\5
$\\{send_val}(\\{cur_val})$;\5
$\\{send_out}(\\{misc},\39\.{"\}"})$;\6
\&{end}\6
\4\&{else} \&{begin} \37$\\{send_out}(\\{misc},\39\.{"["})$;\5
$\\{send_val}(\\{cur_val})$;\5
$\\{send_out}(\\{misc},\39\.{"]"})$;\6
\&{end};\2\par

\U section 99.
\N107.  Introduction to the input phase.
We have now seen that \.{TANGLE} will be able to output the full
\PASCAL\ 
program, if we can only get that program into the byte memory in the
proper
format. The input process is something like the output process in
reverse,
since we compress the text as we read it in and we expand it as we
write it out.

There are three main input routines. The most interesting is the one
that gets
the next token of a \PASCAL\ text; the other two are used to scan
rapidly past
\TEX\ text in the \.{WEB} source code. One of the latter routines will
jump to
the next token that starts with `\.{@}', and the other skips to the end
of a \PASCAL\ comment.


\M108. But first we need to consider the low-level routine that takes
care of
updating page and line numbers for error messages and progress reports.
This routine also makes sure that the input ends with an end-of-page
signal.
The conventions of \TEX's input routine are used in simplified form:
When
$\\{line}=0$, it is time to read a new page. After a line has been
input, either
$\\{buffer}[\\{limit}]$ is a \\{carriage_return}, on a normal line, or
we have
$\\{limit}=0$ and $\\{buffer}[0]=\\{form_feed}$, in which case this is
the line that
ends a page. The value of \\{limit} is always strictly less than \\
{buf_size},
so it is possible to refer to $\\{buffer}[\\{limit}+1]$ without
overstepping the
bounds of the array.

\Y\P$\4\X12:Globals in the outer block\X\mathrel{+}≡$\6
\4\\{page}: \37\\{sixteen_bits};\C{the number of the page currently
being read}\6
\4\\{line}: \37\\{sixteen_bits};\C{the number of the current line on
the current page}\6
\4\\{limit}: \37$0\to\\{buf_size}$;\C{the last character position
occupied in the buffer}\6
\4\\{loc}: \37$0\to\\{buf_size}$;\C{the next character position to be
read from the buffer}\6
\4\\{input_has_ended}: \37\\{boolean};\C{if \\{true}, there is no more
input}\par

\M109. \P$\X109:Initialize the input system\X≡$\6
\&{if} $\\{open_input}$ \1\&{then}\C{something wrong with the \\{input}
file}\6
\&{begin} \37$\\{ex_print_nl}(\.{\'!\ Couldn\'}\.{\'t\ open\ the\
input\ file.\'})$;\5
\\{quit};\6
\&{end};\2\6
$\\{page}←0$;\5
$\\{line}←0$;\5
$\\{limit}←0$;\5
$\\{loc}←1$;\5
$\\{buffer}[0]←\.{"\ "}$;\5
$\\{input_has_ended}←\\{false}$\par
\U section 151.

\M110. The \\{get_line} procedure is called when $\\{loc}>\\{limit}$;
it puts the next
line of input into the buffer and updates the other variables
appropriately.
A carriage return at the end of a line is changed to a space.

\Y\P\4\&{procedure}\1\  \37\\{get_line};\C{inputs the next line}\2\6
\&{begin} \37\&{if} $\\{buffer}[0]=\\{form_feed}$ \1\&{then}\5
$\\{line}←0$;\2\6
\&{if} $\\{input_ln}$ \1\&{then}\C{not end of file}\6
\&{begin} \37\&{if} $\\{line}=0$ \1\&{then}\C{first line of page}\6
\&{begin} \37$\\{incr}(\\{page})$;\5
$\\{print}(\\{page}:0,\39\.{\'\ \'})$;\C{print progress report}\6
\X111:Special check for file directory page\X;\6
\&{end};\2\6
\&{if} $\\{buffer}[\\{limit}]=\\{carriage_return}$ \1\&{then}\5
$\\{buffer}[\\{limit}]←\.{"\ "}$;\2\6
\&{end}\6
\4\&{else} \&{if} $\\{buffer}[0]≠\\{form_feed}$ \1\&{then}\C{insert
page mark at end of file}\6
\&{begin} \37$\\{limit}←0$;\5
$\\{buffer}[0]←\\{form_feed}$;\6
\&{end}\6
\4\&{else} $\\{input_has_ended}←\\{true}$;\C{note that page mark is
still present}\2\2\6
$\\{incr}(\\{line})$;\5
$\\{loc}←0$;\6
\&{end};\par

\M111. The `E' editor on Stanford's {\sc SAIL} computer, where the
present version
of \.{TANGLE} was developed, makes up a special first page called the
`file
directory' for the files it works with.
This first page is recognizable with sufficiently high
probability by the fact that its first line has length 29 and its first
and ninth characters are respectively `\.C' and `\.\⊗'. So the Stanford
version of \.{TANGLE} has special code to skip past all but the page
mark
of such a first page:

\Y\P$\4\X111:Special check for file directory page\X≡$\6
\\{STANFORD} \6
\&{if} $(\\{page}=1)∧(\\{limit}=29)$ \1\&{then}\6
\&{if} $(\\{buffer}[0]=\.{"C"})∧(\\{buffer}[8]=\.{"\⊗"})$ \1\&{then}\6
\1\&{repeat} \37\&{if} $\\{input_ln}$ \1\&{then}\5
\\{do_nothing}\6
\4\&{else} \&{begin} \37$\\{limit}←0$;\5
$\\{buffer}[0]←\\{form_feed}$;\6
\&{end};\2\6
\4\&{until}\5
$\\{buffer}[0]=\\{form_feed}\\{DROFNATS}$\2\2\2\par
\U section 110.

\M112. Important milestones are reached during the input phase when
certain
control codes are sensed, or when a page ends.

Control codes in \.{WEB} begin with `\.{@}', and the next character
identifies the code. Some of these are of interest only to \.{WEAVE},
so \.{TANGLE} ignores them; the others are converted by \.{TANGLE} into
internal code numbers by the \\{control_code} function below. The
ordering
of these internal code numbers has been chosen to simplify the program
logic;
larger numbers are given to the control codes that denote more
significant
milestones.

\Y\P\D \37$\\{ignore}=0$\C{control code of no interest to \.
{TANGLE}}\par
\P\D \37$\\{TEX_string}=\O203$\C{control code for `\.{@t}'}\par
\P\D \37$\\{format}=\O204$\C{control code for `\.{@f}'}\par
\P\D \37$\\{definition}=\O205$\C{control code for `\.{@d}'}\par
\P\D \37$\\{begin_pascal}=\O206$\C{control code for `\.{@p}'}\par
\P\D \37$\\{module_name}=\O207$\C{control code for `\.{@<}'}\par
\P\D \37$\\{page_end}=\O210$\C{control code for end-of-page}\par
\P\D \37$\\{new_module}=\O211$\C{control code for `\.{@ }' and `\.
{@*}'}\par
\Y\P\4\&{function}\1\  \37$\\{control_code}(\\c:\\{ascii_code})$: \37\\
{eight_bits};\C{convert $c$ after \.{@}}\2\6
\&{begin} \37\&{case} $\\c$ \1\&{of}\6
\4\.{"@"}: \37$\\{control_code}←\.{"@"}$;\C{`quoted' at sign}\6
\4\.{"\'"}: \37$\\{control_code}←\\{octal}$;\C{precedes octal
constant}\6
\4$\.{"\ "},\39\\{tab_mark},\39\.{"*"}$: \37$\\{control_code}←\\
{new_module}$;\C{beginning of a new module}\6
\4$\.{"T"},\39\.{"t"}$: \37$\\{control_code}←\\{TEX_string}$;\C{\.
{@t...@>} to be ignored}\6
\4$\.{"D"},\39\.{"d"}$: \37$\\{control_code}←\\{definition}$;\C{macro
definition}\6
\4$\.{"F"},\39\.{"f"}$: \37$\\{control_code}←\\{format}$;\C{format
definition}\6
\4\.{"\{"}: \37$\\{control_code}←\\{begin_comment}$;\C{begin-comment
delimiter}\6
\4\.{"\}"}: \37$\\{control_code}←\\{end_comment}$;\C{end-comment
delimiter}\6
\4$\.{"P"},\39\.{"p"}$: \37$\\{control_code}←\\
{begin_pascal}$;\C{\PASCAL\ text in unnamed module}\6
\4\.{"&"}: \37$\\{control_code}←\\{join}$;\C{concatenate two tokens}\6
\4\.{"<"}: \37$\\{control_code}←\\{module_name}$;\C{beginning of a
module name}\6
\4\&{othercases} \37$\\{control_code}←\\{ignore}$\C{ignore all other
cases}\2\6
\&{endcases};\6
\&{end};\par

\M113. The \\{skip_ahead} procedure reads through the input at fairly
high speed
until finding the next non-ignorable control code, which it returns.

\Y\P\4\&{function}\1\  \37\\{skip_ahead}: \37\\{eight_bits};\C{skip to
next control code}\6
\4\&{label} \37\\{done};\6
\4\&{var} \37\\c: \37\\{eight_bits};\C{control code found}\2\6
\&{begin} \37\&{loop}\1\6
\&{begin} \37\&{if} $\\{loc}>\\{limit}$ \1\&{then}\6
\&{begin} \37\\{get_line};\6
\&{if} $\\{buffer}[0]=\\{form_feed}$ \1\&{then}\6
\&{begin} \37$\\{loc}←1$;\5
$\\c←\\{page_end}$;\5
\&{goto} \37\\{done};\6
\&{end};\2\6
\&{end};\2\6
$\\{buffer}[\\{limit}+1]←\.{"@"}$;\6
\&{while} $\\{buffer}[\\{loc}]≠\.{"@"}$ \1\&{do}\5
$\\{incr}(\\{loc})$;\2\6
\&{if} $\\{loc}≤\\{limit}$ \1\&{then}\6
\&{begin} \37$\\{loc}←\\{loc}+2$;\5
$\\c←\\{control_code}(\\{buffer}[\\{loc}-1])$;\6
\&{if} $(\\c≠\\{ignore})∨(\\{buffer}[\\{loc}-1]=\.{">"})$ \1\&{then}\5
\&{goto} \37\\{done};\2\6
\&{end};\2\6
\&{end};\2\6
\4\\{done}: \37$\\{skip_ahead}←\\c$;\6
\&{end};\par

\M114. The \\{skip_comment} procedure reads through the input at
somewhat high speed
until finding the first unmatched right brace or until coming to the
end
of a page. It ignores characters following `\.\\' characters, since all
braces that aren't nested are supposed to be hidden in that way. For
example, consider the process of skipping the first comment below,
where the string containing the right brace has been typed as \.{\`\\.\\
\}\'}
in the \.{WEB} file.

\Y\P\4\&{procedure}\1\  \37\\{skip_comment};\C{skips to next unmatched
`\.\}'}\6
\4\&{label} \37\\{exit};\6
\4\&{var} \37\\{bal}: \37\\{eight_bits};\C{excess of left braces}\6
\\c: \37\\{ascii_code};\C{current character}\2\6
\&{begin} \37$\\{bal}←0$;\6
\&{loop}\1\6
\&{begin} \37\&{if} $\\{loc}>\\{limit}$ \1\&{then}\6
\&{begin} \37\\{get_line};\6
\&{if} $\\{buffer}[0]=\\{form_feed}$ \1\&{then}\6
\&{begin} \37$\\{ex_err_print}(\.{\'!\ Page\ ended\ in\ mid-comment\'
})$;\5
$\\{loc}←1$;\5
\&{return};\6
\&{end};\2\6
\&{end};\2\6
$\\c←\\{buffer}[\\{loc}]$;\5
$\\{incr}(\\{loc})$;\5
\X115:Do special things when $\\c=\.{"@"},\.{"\\"},\.{"\{"},\.{"\}"}$;
\&{return} at end\X;\6
\&{end};\2\6
\4\\{exit}: \37\&{end};\par

\M115. \P$\X115:Do special things when $\\c=\.{"@"},\.{"\\"},\.{"\{"},\.
{"\}"}$; \&{return} at end\X≡$\6
\&{if} $\\c=\.{"@"}$ \1\&{then}\6
\&{begin} \37$\\c←\\{buffer}[\\{loc}]$;\6
\&{if} $(\\c≠\.{"\ "})∧(\\c≠\\{tab_mark})∧(\\c≠\.{"*"})$ \1\&{then}\5
$\\{incr}(\\{loc})$\6
\4\&{else} \&{begin} \37$\\{ex_err_print}(\.{\'!\ Module\ ended\ in\
mid-comment\'})$;\5
$\\{decr}(\\{loc})$;\5
\&{return};\6
\&{end}\2\6
\&{end}\6
\4\&{else} \&{if} $(\\c=\.{"\\"})∧(\\{buffer}[\\{loc}]≠\.{"@"})$ \1\&
{then}\5
$\\{incr}(\\{loc})$\6
\4\&{else} \&{if} $\\c=\.{"\{"}$ \1\&{then}\5
$\\{incr}(\\{bal})$\6
\4\&{else} \&{if} $\\c=\.{"\}"}$ \1\&{then}\6
\&{begin} \37\&{if} $\\{bal}=0$ \1\&{then}\5
\&{return};\2\6
$\\{decr}(\\{bal})$;\6
\&{end}\2\2\2\2\par

\U section 114.
\N116.  Inputting the next token.
As stated above, \.{TANGLE}'s most interesting input procedure is the
\\{get_next} routine that inputs the next token. However, the procedure
isn't especially difficult.

In most cases the tokens output by \\{get_next} have the form used in
replacement texts, except that two-byte tokens are not produced.
An identifier that isn't one letter long is represented by the
output `\\{identifier}', and in such a case the global variables
\\{id_first} and \\{id_loc} will have been set to the appropriate
values
needed by the \\{id_lookup} procedure. A string that begins with a
double-quote is also considered an \\{identifier}, and in such a case
the global variable \\{double_chars} will also have been set
appropriately.
Control codes produce the corresponding output of the \\{control_code}
function above; and if that code is \\{module_name}, the value of \\
{cur_module}
will point to the \\{byte_start} entry for that module name.

\Y\P$\4\X12:Globals in the outer block\X\mathrel{+}≡$\6
\4\\{cur_module}: \37\\{name_pointer};\C{name of module just
scanned}\par

\M117. \P\6
\4\&{function}\1\  \37\\{get_next}: \37\\{eight_bits};\C{produces the
next input token}\6
\4\&{label} \37$\\{restart},\39\\{done}$;\6
\4\&{var} \37\\c: \37\\{eight_bits};\C{the current character}\6
\\d: \37\\{eight_bits};\C{the next character}\6
$\\j,\39\\k$: \37$0\to\\{longest_name}$;\C{indices into \\{module}}\6
\&{begin} \37\\{restart}: \37\&{if} $\\{loc}>\\{limit}$ \1\&{then}\5
\\{get_line};\2\6
$\\c←\\{buffer}[\\{loc}]$;\5
$\\{incr}(\\{loc})$;\6
\&{case} $\\c$ \1\&{of}\6
\4$\.{"A"},\39\\{up_to}(\.{"Z"}),\39\.{"a"},\39\\{up_to}(\.{"z"})$: \3
7\X119:Get an identifier\X;\6
\4\.{""}\.{""}: \37\X120:Get a preprocessed string\X;\6
\4\.{"@"}: \37\X121:Get control code and possible module name\X;\6
\4$\X118:Compress two-symbol combinations like `\.{:=}'\X\.{"\ "},\39\\
{tab_mark}$: \37\&{goto} \37\\{restart};\C{ignore spaces and tabs}\6
\4\.{"\{"}: \37\&{begin} \37\\{skip_comment};\5
\&{goto} \37\\{restart};\6
\&{end};\6
\4\\{form_feed}: \37$\\c←\\{page_end}$;\6
\4\&{othercases} \37\\{do_nothing}\2\6
\&{endcases}; \\{DEBUG} \6
\&{if} $\\{trouble_shooting}$ \1\&{then}\5
\\{debug_help};\2\6
$\\{GUBED}\\{get_next}←\\c$; \6
\&{end} ;\par

\M118. Note that the following code substitutes \.{@\{} and \.{@\}} for
the
respective combinations `\.{(*}' and `\.{*)}'. Explicit braces should
be used
for \TEX\ comments in \PASCAL\ text.

\Y\P\D $\\{compress}(\#)≡$ \6
\&{begin} \37$\\c←\#$;\5
$\\{incr}(\\{loc})$;\6
\&{end}\par
\Y\P$\4\X118:Compress two-symbol combinations like `\.{:=}'\X≡$\6
\4\.{"."}: \37\&{if} $\\{buffer}[\\{loc}]=\.{"."}$ \1\&{then}\5
$\\{compress}(\\{double_dot})$;\2\6
\4\.{":"}: \37\&{if} $\\{buffer}[\\{loc}]=\.{"="}$ \1\&{then}\5
$\\{compress}(\\{left_arrow})$;\2\6
\4\.{"="}: \37\&{if} $\\{buffer}[\\{loc}]=\.{"="}$ \1\&{then}\5
$\\{compress}(\\{equivalence_sign})$;\2\6
\4\.{">"}: \37\&{if} $\\{buffer}[\\{loc}]=\.{"="}$ \1\&{then}\5
$\\{compress}(\\{greater_or_equal})$;\2\6
\4\.{"<"}: \37\&{if} $\\{buffer}[\\{loc}]=\.{"="}$ \1\&{then}\5
$\\{compress}(\\{less_or_equal})$\6
\4\&{else} \&{if} $\\{buffer}[\\{loc}]=\.{">"}$ \1\&{then}\5
$\\{compress}(\\{not_equal})$;\2\2\6
\4\.{"("}: \37\&{if} $\\{buffer}[\\{loc}]=\.{"*"}$ \1\&{then}\5
$\\{compress}(\\{begin_comment})$;\2\6
\4\.{"*"}: \37\&{if} $\\{buffer}[\\{loc}]=\.{")"}$ \1\&{then}\5
$\\{compress}(\\{end_comment})$;\2\par
\U section 117.

\M119. \P$\X119:Get an identifier\X≡$\6
\&{begin} \37$\\{decr}(\\{loc})$;\5
$\\{id_first}←\\{loc}$;\6
\1\&{repeat} \37$\\{incr}(\\{loc})$;\5
$\\d←\\{buffer}[\\{loc}]$;\6
\4\&{until}\5
$((\\d<\.{"0"})∨((\\d>\.{"9"})∧(\\d<\.{"A"}))∨((\\d>\.{"Z"})∧(\\d<\.
{"a"}))∨(\\d>\.{"z"}))∧(\\d≠\.{"_"})$;\2\6
\&{if} $\\{loc}>\\{id_first}+1$ \1\&{then}\6
\&{begin} \37$\\c←\\{identifier}$;\5
$\\{id_loc}←\\{loc}$;\6
\&{end};\2\6
\&{end}\par
\U section 117.

\M120. A string that starts and ends with double-quote marks is
converted into
an identifier that behaves like a numeric macro by means of the
following
piece of the program.

\Y\P$\4\X120:Get a preprocessed string\X≡$\6
\&{begin} \37$\\{double_chars}←0$;\5
$\\{id_first}←\\{loc}-1$;\6
\1\&{repeat} \37$\\d←\\{buffer}[\\{loc}]$;\5
$\\{incr}(\\{loc})$;\6
\&{if} $(\\d=\.{""}\.{""})∨(\\d=\.{"@"})$ \1\&{then}\6
\&{if} $\\{buffer}[\\{loc}]=\\d$ \1\&{then}\6
\&{begin} \37$\\{incr}(\\{loc})$;\5
$\\d←0$;\5
$\\{incr}(\\{double_chars})$;\6
\&{end}\6
\4\&{else} \&{if} $\\d=\.{"@"}$ \1\&{then}\5
$\\{err_print}(\.{\'!\ Double\ @\ sign\ missing\'})$\6
\4\&{else} \&{if} $\\{loc}>\\{limit}$ \1\&{then}\6
\&{begin} \37$\\{ex_err_print}(\.{\'!\ String\ constant\ didn\'}\.{\'t\
end\'})$;\5
$\\d←\.{""}\.{""}$;\6
\&{end};\2\2\2\2\6
\4\&{until}\5
$\\d=\.{""}\.{""}$;\2\6
$\\{id_loc}←\\{loc}-1$;\5
$\\c←\\{identifier}$;\6
\&{end}\par
\U section 117.

\M121. After an \.{@} sign has been scanned, the next character tells
us
whether there is more work to do.

\Y\P$\4\X121:Get control code and possible module name\X≡$\6
\&{begin} \37$\\c←\\{control_code}(\\{buffer}[\\{loc}])$;\5
$\\{incr}(\\{loc})$;\6
\&{if} $\\c=\\{ignore}$ \1\&{then}\5
\&{goto} \37\\{restart}\6
\4\&{else} \&{if} $\\c=\\{module_name}$ \1\&{then}\5
\X122:Scan the module name and make \\{cur_module} point to it\X\6
\4\&{else} \&{if} $\\c=\\{TEX_string}$ \1\&{then}\6
\&{begin} \37\1\&{repeat} \37$\\c←\\{skip_ahead}$;\6
\4\&{until}\5
$\\c≠\.{"@"}$;\2\6
\&{if} $\\{buffer}[\\{loc}-1]≠\.{">"}$ \1\&{then}\5
$\\{err_print}(\.{\'!\ Improper\ @\ within\ @t...@>\'})$;\2\6
\&{goto} \37\\{restart};\6
\&{end};\2\2\2\6
\&{end}\par
\U section 117.

\M122. \P$\X122:Scan the module name and make \\{cur_module} point to
it\X≡$\6
\&{begin} \37\X124:Put module name into $\\{module}[1\to\\k]$\X;\6
\&{if} $\\k>3$ \1\&{then}\6
\&{begin} \37\&{if} $(\\{module}[\\k]=\.{"."})∧(\\{module}[\\k-1]=\.
{"."})∧(\\{module}[\\k-2]=\.{"."})$ \1\&{then}\5
$\\{cur_module}←\\{prefix_lookup}(\\k-3)$\6
\4\&{else} $\\{cur_module}←\\{mod_lookup}(\\k)$;\2\6
\&{end}\6
\4\&{else} $\\{cur_module}←\\{mod_lookup}(\\k)$;\2\6
\&{end}\par
\U section 121.

\M123. Module names are placed into the \\{module} array with
consecutive spaces,
tabs, and carriage-returns replaced by single spaces. There will be no
spaces at the beginning or the end. (We set $\\{module}[0]←\.{"\ "}$ to
facilitate
this, since the \\{mod_lookup} routine uses $\\{module}[1]$ as the
first
character of the name.)

\Y\P$\4\X13:Set initial values\X\mathrel{+}≡$\6
$\\{module}[0]←\.{"\ "}$;\par

\M124. \P$\X124:Put module name into $\\{module}[1\to\\k]$\X≡$\6
$\\k←0$;\6
\&{loop}\1\6
\&{begin} \37\&{if} $\\{loc}>\\{limit}$ \1\&{then}\6
\&{begin} \37\\{get_line};\6
\&{if} $\\{buffer}[0]=\\{form_feed}$ \1\&{then}\6
\&{begin} \37$\\{ex_err_print}(\.{\'!\ Page\ ended\ in\ module\ name\'
})$;\5
$\\{loc}←1$;\5
\&{goto} \37\\{done};\6
\&{end};\2\6
\&{end};\2\6
$\\d←\\{buffer}[\\{loc}]$;\5
\X125:if end of name, \&{goto} \\{done}\X;\6
$\\{incr}(\\{loc})$;\6
\&{if} $\\k<\\{longest_name}-1$ \1\&{then}\5
$\\{incr}(\\k)$;\2\6
\&{if} $(\\d=\.{"\ "})∨(\\d=\\{tab_mark})$ \1\&{then}\6
\&{begin} \37$\\d←\.{"\ "}$;\6
\&{if} $\\{module}[\\k-1]=\.{"\ "}$ \1\&{then}\5
$\\{decr}(\\k)$;\2\6
\&{end};\2\6
$\\{module}[\\k]←\\d$;\6
\&{end};\2\6
\4\\{done}: \37\X126:Check for overlong name\X;\6
\&{if} $(\\{module}[\\k]=\.{"\ "})∧(\\k>0)$ \1\&{then}\5
$\\{decr}(\\k)$\2\par
\U section 122.

\M125. \P$\X125:if end of name, \&{goto} \\{done}\X≡$\6
\&{if} $\\d=\.{"@"}$ \1\&{then}\6
\&{begin} \37$\\d←\\{buffer}[\\{loc}+1]$;\6
\&{if} $\\d=\.{">"}$ \1\&{then}\6
\&{begin} \37$\\{loc}←\\{loc}+2$;\5
\&{goto} \37\\{done};\6
\&{end};\2\6
\&{if} $(\\d=\.{"\ "})∨(\\d=\\{tab_mark})∨(\\d=\.{"*"})$ \1\&{then}\6
\&{begin} \37$\\{ex_err_print}(\.{\'!\ Module\ name\ didn\'}\.{\'t\
end\'})$;\5
\&{goto} \37\\{done};\6
\&{end};\2\6
$\\{incr}(\\k)$;\5
$\\{module}[\\k]←\.{"@"}$;\5
$\\{incr}(\\{loc})$;\C{now $\\d=\\{buffer}[\\{loc}]$ again}\6
\&{end}\2\par
\U section 124.

\M126. \P$\X126:Check for overlong name\X≡$\6
\&{if} $\\k≥\\{longest_name}-2$ \1\&{then}\6
\&{begin} \37$\\{ex_print_nl}(\.{\'!\ Module\ name\ too\ long:\ \'})$;\6
\&{for} $\\j←1\mathrel{\&{to}}25$ \1\&{do}\5
$\\{print}(\\{chr}(\\{module}[\\j]))$;\2\6
$\\{print}(\.{\'...\'})$;\6
\&{end}\2\par

\U section 124.
\N127.  Scanning a numeric definition.
When \.{TANGLE} looks at the \PASCAL\ text following the `\.=' of a
numeric
macro definition, it calls on the precedure $\\{scan_numeric}(\\p)$,
where $p$
points to the name that is to be defined. This procedure evaluates the
right-hand side, which must consist entirely of integer constants and
defined numeric macros connected with \.+ and \.- signs (no
parentheses).
It also sets the global variable \\{next_control} to the control code
that
terminated this definition.

A definition ends with the control codes \\{definition}, \\{format}, \\
{module_name},
\\{begin_pascal}, \\{new_module}, and \\{page_end}, all of which can be
recognized
by the fact that they are the largest values \\{get_next} can return.

\Y\P\D \37$\\{end_of_definition}(\#)≡(\#≥\\{format})$\C{is $\#$ a
control code ending a definition?}\par
\Y\P$\4\X12:Globals in the outer block\X\mathrel{+}≡$\6
\4\\{next_control}: \37\\{eight_bits};\C{control code waiting to be
acted upon}\par

\M128. The evaluation of a numeric expression makes use of two
variables called the
\\{accumulator} and the \\{next_sign}. At the beginning, \\
{accumulator} is zero and
\\{next_sign} is $+1$. When a \.+ or \.- is scanned, \\{next_sign} is
multiplied
by the value of that sign. When a numeric value is scanned, it is
multiplied
\\{next_sign} and added to the \\{accumulator}, then \\{next_sign} is
reset to $+1$.

\Y\P\4\&{procedure}\1\  \37$\\{scan_numeric}(\\p:\\
{name_pointer})$;\C{defines numeric macros}\6
\4\&{label} \37$\\{reswitch},\39\\{done}$;\6
\4\&{var} \37\\{accumulator}: \37\\{integer};\C{accumulates sums}\6
\\{next_sign}: \37$-1\to+1$;\C{sign to attach to next value}\6
\\q: \37\\{name_pointer};\C{points to identifiers being evaluated}\6
\\{val}: \37\\{integer};\C{constants being evaluated}\6
\4\&{procedure}\1\  \37$\\{add_in}(\\v:\\{integer})$;\C{do this when a
new value comes in}\2\6
\&{begin} \37$\\{accumulator}←\\{accumulator}+\\{next_sign}\ast\\v$;\5
$\\{next_sign}←+1$;\6
\&{end};\2\6
\&{begin} \37\X129:Set \\{accumulator} to the value of the right-hand
side\X;\6
\&{if} $\\{abs}(\\{accumulator})≥\O100000$ \1\&{then}\6
\&{begin} \37$\\{ex_err_print}(\.{\'!\ Value\ too\ big:\ \'},\39\\
{accumulator}:0)$;\5
$\\{accumulator}←0$;\6
\&{end};\2\6
$\\{equiv}[\\p]←\\{accumulator}+\O100000$;\C{name $p$ now is defined to
equal \\{accumulator}}\6
\&{end};\par

\M129. \P$\X129:Set \\{accumulator} to the value of the right-hand
side\X≡$\6
$\\{accumulator}←0$;\5
$\\{next_sign}←+1$;\6
\&{loop}\1\6
\&{begin} \37$\\{next_control}←\\{get_next}$;\6
\4\\{reswitch}: \37\&{case} $\\{next_control}$ \1\&{of}\6
\4\\{digits}: \37\&{begin} \37\X131:Set \\{val} to value of decimal
constant, and set \\{next_control} to the following token\X;\6
$\\{add_in}(\\{val})$;\5
\&{goto} \37\\{reswitch};\6
\&{end};\6
\4\\{octal}: \37\&{begin} \37\X132:Set \\{val} to value of octal
constant, and set \\{next_control} to the following token\X;\6
$\\{add_in}(\\{val})$;\5
\&{goto} \37\\{reswitch};\6
\&{end};\6
\4\\{identifier}: \37\&{begin} \37$\\q←\\{id_lookup}(\\{normal})$;\6
\&{if} $\\{ilk}[\\q]≠\\{numeric}$ \1\&{then}\6
\&{begin} \37$\\{next_control}←\.{"*"}$;\5
\&{goto} \37\\{reswitch};\C{leads to error}\6
\&{end};\2\6
$\\{add_in}(\\{equiv}[\\q]-\O100000)$;\6
\&{end};\6
\4\.{"+"}: \37\\{do_nothing};\6
\4\.{"-"}: \37$\\{next_sign}←-\\{next_sign}$;\6
\4$\\{format},\39\\{definition},\39\\{module_name},\39\\
{begin_pascal},\39\\{page_end},\39\\{new_module}$: \37\&{goto} \37\\
{done};\6
\4\.{";"}: \37$\\{err_print}(\.{\'!\ Omit\ semicolon\ in\ numeric\
definition\'})$;\6
\4\&{othercases} \37\X130:Signal error, flush rest of the definition\X\2
\6
\&{endcases};\6
\&{end}\6
\4\\{done}: \37\2\par
\U section 128.

\M130. \P$\X130:Signal error, flush rest of the definition\X≡$\6
\&{begin} \37$\\{ex_err_print}(\.{\'!\ Improper\ numeric\ definition\
will\ be\ flushed\'})$;\6
\1\&{repeat} \37$\\{next_control}←\\{skip_ahead}$\6
\4\&{until}\5
$\\{end_of_definition}(\\{next_control})$;\2\6
\&{if} $\\{next_control}=\\{module_name}$ \1\&{then}\6
\&{begin} \37\C{we want to scan the module name too}\6
$\\{loc}←\\{loc}-2$;\5
$\\{next_control}←\\{get_next}$;\6
\&{end};\2\6
$\\{accumulator}←0$;\5
\&{goto} \37\\{done};\6
\&{end}\par
\U section 129.

\M131. \P$\X131:Set \\{val} to value of decimal constant, and set \\
{next_control} to the following token\X≡$\6
$\\{val}←0$;\6
\1\&{repeat} \37$\\{val}←10\ast\\{val}+\\{next_control}-\.{"0"}$;\5
$\\{next_control}←\\{get_next}$;\6
\4\&{until}\5
$(\\{next_control}>\.{"9"})∨(\\{next_control}<\.{"0"})$\2\par
\U section 129.

\M132. \P$\X132:Set \\{val} to value of octal constant, and set \\
{next_control} to the following token\X≡$\6
$\\{val}←0$;\5
$\\{next_control}←\.{"0"}$;\6
\1\&{repeat} \37$\\{val}←8\ast\\{val}+\\{next_control}-\.{"0"}$;\5
$\\{next_control}←\\{get_next}$;\6
\4\&{until}\5
$(\\{next_control}>\.{"7"})∨(\\{next_control}<\.{"0"})$\2\par

\U section 129.
\N133.  Scanning a macro definition.
The rules for generating the replacement texts corresponding to simple
macros, parametric macros, and \PASCAL\ texts of a module are almost
identical, so a single procedure is used for all three cases. The
differences are that

\yskip\item{a)} The sign $\#$ denotes a parameter only when it appears
outside of strings in a parametric macro; otherwise it stands for the
ascii character $\#$. (This is not used in standard \PASCAL, but some
\PASCAL s allow, for example, `\.{/\#}' after a certain kind of file
name.)

\item{b)}Module names are not allowed in simple macros or parametric
macros;
in fact, the appearance of a module name terminates such macros and
denotes
the name of the current module.

\item{c)}The symbols \.{@d} and \.{@f} and \.{@p} are not allowed after
module names, while they terminate macro definitions.


\M134. Therefore there is a procedure \\{scan_repl} whose parameter $t$
specifies
either \\{simple} or \\{parametric} or \\{module_name}. After \\
{scan_repl} has
acted, \\{cur_repl_text} will point to the replacement text just
generated, and
\\{next_control} will contain the control code that terminated the
activity.

\Y\P$\4\X12:Globals in the outer block\X\mathrel{+}≡$\6
\4\\{cur_repl_text}: \37\\{text_pointer};\C{replacement text formed by
\\{scan_repl}}\par

\M135. \P\6
\4\&{procedure}\1\  \37$\\{scan_repl}(\\t:\\{eight_bits})$;\C{creates a
replacement text}\6
\4\&{label} \37$\\{continue},\39\\{done},\39\\{found}$;\6
\4\&{var} \37\\a: \37\\{sixteen_bits};\C{the current token}\6
\\b: \37\\{ascii_code};\C{a character from the buffer}\6
\\{bal}: \37\\{eight_bits};\C{left parentheses minus right
parentheses}\2\6
\&{begin} \37$\\{bal}←0$;\6
\&{loop}\1\6
\&{begin} \37\\{continue}: \37$\\a←\\{get_next}$;\6
\&{case} $\\a$ \1\&{of}\6
\4\.{"("}: \37$\\{incr}(\\{bal})$;\6
\4\.{")"}: \37\&{if} $\\{bal}=0$ \1\&{then}\5
$\\{err_print}(\.{\'!\ Extra\ )\'})$\6
\4\&{else} $\\{decr}(\\{bal})$;\2\6
\4\.{"\'"}: \37\X138:Copy a string from the buffer to \\{tok_mem}\X;\6
\4\.{"\#"}: \37\&{if} $\\t=\\{parametric}$ \1\&{then}\5
$\\a←\\{param}$;\2\6
\X137:In cases that $a$ is a non-ascii token (\\{identifier}, \\
{module_name}, etc.), either process it and change $a$ to a byte that
should be stored, or \&{goto} \\{continue} if $a$ should be ignored, or
\&{goto} \\{done} if $a$ signals the end of this replacement text\X\6
\4\&{othercases} \37\\{do_nothing}\2\6
\&{endcases};\5
$\\{app_repl}(\\a)$;\C{store $a$ in \\{tok_mem}}\6
\&{end};\2\6
\4\\{done}: \37$\\{next_control}←\\a$;\5
\X136:Make sure the parentheses balance\X;\6
\&{if} $\\{text_ptr}=\\{max_texts}$ \1\&{then}\5
$\\{overflow}(\.{\'text\'})$;\2\6
$\\{cur_repl_text}←\\{text_ptr}$;\5
$\\{incr}(\\{text_ptr})$;\5
$\\{tok_start}[\\{text_ptr}]←\\{tok_ptr}$;\6
\&{end};\par

\M136. \P$\X136:Make sure the parentheses balance\X≡$\6
\&{if} $\\{bal}>0$ \1\&{then}\6
\&{begin} \37$\\{ex_err_print}(\.{\'!\ Missing\ \'},\39\\{bal}:0,\39\.
{\'\ )\'})$;\6
\&{while} $\\{bal}>0$ \1\&{do}\6
\&{begin} \37$\\{app_repl}(\.{")"})$;\5
$\\{decr}(\\{bal})$;\6
\&{end};\2\6
\&{end}\2\par
\U section 135.

\M137. \P$\X137:In cases that $a$ is a non-ascii token (\\{identifier},
\\{module_name}, etc.), either process it and change $a$ to a byte that
should be stored, or \&{goto} \\{continue} if $a$ should be ignored, or
\&{goto} \\{done} if $a$ signals the end of this replacement text\X≡$\6
\4\\{identifier}: \37\&{begin} \37$\\a←\\{id_lookup}(\\{normal})$;\5
$\\{app_repl}((\\a\mathbin{\&{div}}\O400)+\O200)$;\5
$\\a←\\a\mathbin{\&{mod}}\O400$;\6
\&{end};\6
\4\\{module_name}: \37\&{if} $\\t≠\\{module_name}$ \1\&{then}\5
\&{goto} \37\\{done}\6
\4\&{else} \&{begin} \37$\\{app_repl}((\\{cur_module}\mathbin{\&
{div}}\O400)+\O250)$;\5
$\\a←\\{cur_module}\mathbin{\&{mod}}\O400$;\6
\&{end};\2\6
\4$\\{definition},\39\\{format},\39\\{begin_pascal}$: \37\&{if} $\\t≠\\
{module_name}$ \1\&{then}\5
\&{goto} \37\\{done}\6
\4\&{else} \&{begin} \37$\\{ex_err_print}(\.{\'!\ @\'},\39\\{chr}(\\
{buffer}[\\{loc}-1]),\39\.{\'\ is\ ignored\ in\ PASCAL\ text\'})$;\5
\&{goto} \37\\{continue};\6
\&{end};\2\6
\4$\\{page_end},\39\\{new_module}$: \37\&{goto} \37\\{done};\par
\U section 135.

\M138. \P$\X138:Copy a string from the buffer to \\{tok_mem}\X≡$\6
\&{begin} \37$\\b←\.{"\'"}$;\6
\&{loop}\1\6
\&{begin} \37$\\{app_repl}(\\b)$;\6
\&{if} $\\b=\.{"@"}$ \1\&{then}\6
\&{if} $\\{buffer}[\\{loc}]=\.{"@"}$ \1\&{then}\5
$\\{incr}(\\{loc})$\C{store only one \.{@}}\6
\4\&{else} $\\{err_print}(\.{\'!\ You\ should\ double\ @\ signs\ in\
strings\'})$;\2\2\6
\&{if} $\\{loc}=\\{limit}$ \1\&{then}\6
\&{begin} \37$\\{ex_err_print}(\.{\'!\ String\ didn\'}\.{\'t\ end\'
})$;\5
$\\{buffer}[\\{loc}]←\.{"\'"}$;\5
$\\{buffer}[\\{loc}+1]←0$;\6
\&{end};\2\6
$\\b←\\{buffer}[\\{loc}]$;\5
$\\{incr}(\\{loc})$;\6
\&{if} $\\b=\.{"\'"}$ \1\&{then}\6
\&{begin} \37\&{if} $\\{buffer}[\\{loc}]≠\.{"\'"}$ \1\&{then}\5
\&{goto} \37\\{found}\6
\4\&{else} \&{begin} \37$\\{incr}(\\{loc})$;\5
$\\{app_repl}(\.{"\'"})$;\6
\&{end};\2\6
\&{end};\2\6
\&{end};\2\6
\4\\{found}: \37\&{end}\C{now \\a holds the final \.{"\'"} that will be
stored}\par
\U section 135.

\M139. The following procedure is used to define a simple or parametric
macro,
just after the `\.{==}' of its definition has been scanned.

\Y\P\4\&{procedure}\1\  \37$\\{define_macro}(\\t:\\{eight_bits})$;\6
\4\&{var} \37\\p: \37\\{name_pointer};\C{the identifier being defined}\2
\6
\&{begin} \37$\\p←\\{id_lookup}(\\t)$;\5
$\\{scan_repl}(\\t)$;\6
$\\{equiv}[\\p]←\\{cur_repl_text}$;\5
$\\{text_link}[\\{cur_repl_text}]←0$;\6
\&{end};\par
\N140.  Scanning a module.
The \\{scan_module} procedure starts when `\.{@ }' has been sensed in
the
input, and it proceeds until the end of that module, i.e., until the
end of the page or the next `\.{@ }'. It uses \\{module_count} to keep
track of the current module number; with luck, \.{WEAVE} and \.{TANGLE}
will both assign the same numbers to modules.

\Y\P$\4\X12:Globals in the outer block\X\mathrel{+}≡$\6
\4\\{module_count}: \37$0\to\O27777$;\C{the current module number}\par

\M141. \P\6
\4\&{procedure}\1\  \37\\{scan_module};\6
\4\&{label} \37$\\{done},\39\\{exit}$;\6
\4\&{var} \37\\p: \37\\{name_pointer};\C{module name for the current
module}\2\6
\&{begin} \37$\\{incr}(\\{module_count})$;\5
\X142:Scan the definition part of the current module\X;\6
\X144:Scan the \PASCAL\ part of the current module\X;\6
\4\\{exit}: \37\&{end};\par

\M142. \P$\X142:Scan the definition part of the current module\X≡$\6
$\\{next_control}←0$;\6
\&{loop}\1\6
\&{begin} \37\\{continue}: \37\&{while} $\\{next_control}≤\\{format}$ \1
\&{do}\6
\&{begin} \37$\\{next_control}←\\{skip_ahead}$;\6
\&{if} $\\{next_control}=\\{module_name}$ \1\&{then}\6
\&{begin} \37\C{we want to scan the module name too}\6
$\\{loc}←\\{loc}-2$;\5
$\\{next_control}←\\{get_next}$;\6
\&{end};\2\6
\&{end};\2\6
\&{if} $\\{next_control}≠\\{definition}$ \1\&{then}\5
\&{goto} \37\\{done};\2\6
$\\{next_control}←\\{get_next}$;\C{get identifier name}\6
\&{if} $\\{next_control}≠\\{identifier}$ \1\&{then}\6
\&{begin} \37$\\{ex_err_print}(\.{\'!\ Definition\ flushed,\ must\
start\ with\ \'},\39\.{\'identifier\ of\ length\ >\ 1\'})$;\5
\&{goto} \37\\{continue};\6
\&{end};\2\6
$\\{next_control}←\\{get_next}$;\C{get token after the identifier}\6
\&{if} $\\{next_control}=\.{"="}$ \1\&{then}\6
\&{begin} \37$\\{scan_numeric}(\\{id_lookup}(\\{numeric}))$;\5
\&{goto} \37\\{continue};\6
\&{end}\6
\4\&{else} \&{if} $\\{next_control}=\\{equivalence_sign}$ \1\&{then}\6
\&{begin} \37$\\{define_macro}(\\{simple})$;\5
\&{goto} \37\\{continue};\6
\&{end}\6
\4\&{else} \X143:if the next text is `$(\#)≡$', call \\{define_macro}
and \&{goto} \\{continue}\X;\2\2\6
$\\{ex_err_print}(\.{\'!\ Definition\ flushed\ since\ it\ starts\
badly\'})$;\6
\&{end}\6
\4\\{done}: \37\2\par
\U section 141.

\M143. \P$\X143:if the next text is `$(\#)≡$', call \\{define_macro}
and \&{goto} \\{continue}\X≡$\6
\&{if} $\\{next_control}=\.{"("}$ \1\&{then}\6
\&{begin} \37$\\{next_control}←\\{get_next}$;\6
\&{if} $\\{next_control}=\.{"\#"}$ \1\&{then}\6
\&{begin} \37$\\{next_control}←\\{get_next}$;\6
\&{if} $\\{next_control}=\.{")"}$ \1\&{then}\6
\&{begin} \37$\\{next_control}←\\{get_next}$;\6
\&{if} $\\{next_control}=\.{"="}$ \1\&{then}\6
\&{begin} \37$\\{ex_err_print}(\.{\'!\ Use\ ==\ for\ macros\'})$;\5
$\\{next_control}←\\{equivalence_sign}$;\6
\&{end};\2\6
\&{if} $\\{next_control}=\\{equivalence_sign}$ \1\&{then}\6
\&{begin} \37$\\{define_macro}(\\{parametric})$;\5
\&{goto} \37\\{continue};\6
\&{end};\2\6
\&{end};\2\6
\&{end};\2\6
\&{end}\2\par
\U section 142.

\M144. \P$\X144:Scan the \PASCAL\ part of the current module\X≡$\6
\&{case} $\\{next_control}$ \1\&{of}\6
\4\\{begin_pascal}: \37$\\p←0$;\6
\4\\{module_name}: \37\&{begin} \37$\\p←\\{cur_module}$;\5
\X145:Check that $=$ or $≡$ follows this module name, otherwise \&
{return}\X;\6
\&{end};\6
\4\&{othercases} \37\&{return}\2\6
\&{endcases};\5
\X146:Insert the module number into \\{tok_mem}\X;\6
$\\{scan_repl}(\\{module_name})$;\C{now \\{cur_repl_txt} points to the
replacement text}\6
\X147:Update the data structure so that the replacement text is
accessible\X;\par
\U section 141.

\M145. \P$\X145:Check that $=$ or $≡$ follows this module name,
otherwise \&{return}\X≡$\6
\1\&{repeat} \37$\\{next_control}←\\{get_next}$;\6
\4\&{until}\5
$\\{next_control}≠\.{"+"}$;\C{allow optional `\.{+=}'}\2\6
\&{if} $(\\{next_control}≠\.{"="})∧(\\{next_control}≠\\
{equivalence_sign})$ \1\&{then}\6
\&{begin} \37$\\{ex_err_print}(\.{\'!\ PASCAL\ text\ flushed,\ =\ sign\
is\ missing\'})$;\6
\1\&{repeat} \37$\\{next_control}←\\{skip_ahead}$;\6
\4\&{until}\5
$\\{next_control}≥\\{page_end}$;\2\6
\&{return};\6
\&{end}\2\par
\U section 144.

\M146. \P$\X146:Insert the module number into \\{tok_mem}\X≡$\6
$\\{store_two_bytes}(\O150000+\\
{module_count})$\C{$\O150000=\O320\ast\O400$}\par
\U section 144.

\M147. \P$\X147:Update the data structure so that the replacement text
is accessible\X≡$\6
\&{if} $\\p=0$ \1\&{then}\C{unnamed module}\6
\&{begin} \37$\\{text_link}[\\{last_unnamed}]←\\{cur_repl_text}$;\5
$\\{last_unnamed}←\\{cur_repl_text}$;\6
\&{end}\6
\4\&{else} \&{if} $\\{equiv}[\\p]=0$ \1\&{then}\5
$\\{equiv}[\\p]←\\{cur_repl_text}$\C{first module of this name}\6
\4\&{else} \&{begin} \37$\\p←\\{equiv}[\\p]$;\6
\&{while} $\\{text_link}[\\p]<\\{module_flag}$ \1\&{do}\5
$\\p←\\{text_link}[\\p]$;\C{find end of list}\2\6
$\\{text_link}[\\p]←\\{cur_repl_text}$;\6
\&{end};\2\2\6
$\\{text_link}[\\{cur_repl_text}]←\\{module_flag}$\C{mark this
replacement text as a nonmacro}\par

\U section 144.
\N148.  Debugging.
The \PASCAL\ debugger with which \.{TANGLE} was developed allows
breakpoints
to be set, and variables can be read and changed, but procedures cannot
be
executed. Therefore a `\\{debug_help}' procedure has been inserted in
the main
loops of both phases of the program; when \\{ddt} and \\{dd} are set to
appropriate
values, symbolic printouts of various tables will appear.

The idea is to set a breakpoint inside the \\{debug_help} routine.
Then when \\{debug_help} is to be activated, set \\{trouble_shooting}
equal to \\{true}, and set \\{ddt} and \\{dd} for the desired actions.

\Y\P$\4\X12:Globals in the outer block\X\mathrel{+}≡$\6
\4\\{DEBUG}\\{trouble_shooting}: \37\\{boolean};\C{is \\{debug_help}
wanted?}\6
\4\\{ddt}: \37\\{sixteen_bits};\C{operation code for the \\{debug_help}
routine}\6
\4\\{dd}: \37\\{sixteen_bits};\C{operand in procedures performed by \\
{debug_help}}\6
\\{GUBED}\par

\M149. \P$\X13:Set initial values\X\mathrel{+}≡$\6
$\\{DEBUG}\\{trouble_shooting}←\\{true}$;\5
$\\{ddt}←9999$;\ \\{GUBED}\par

\M150. 
\Y\P\D \37$\\{breakpoint}=888$\C{label where breakpoint is
desirable}\par
\Y\P\\{DEBUG} \6
\4\&{procedure}\1\  \37\\{debug_help};\C{routine to display various
things}\6
\4\&{label} \37\\{breakpoint};\6
\4\&{var} \37\\k: \37\\{sixteen_bits};\2\6
\&{begin} \37\&{while} $\\{ddt}≠0$ \1\&{do}\6
\&{begin} \37\\{breakpoint}: \37\C{debugger stops here allowing changes
to $\\{ddt},\\{dd}$}\6
\&{case} $\\{ddt}$ \1\&{of}\6
\40: \37\\{do_nothing};\6
\41: \37$\\{print_id}(\\{dd})$;\6
\42: \37$\\{print_repl}(\\{dd})$;\6
\43: \37$\\{err_print}(\.{\'*\'})$;\C{shows input or output buffer}\6
\44: \37\&{for} $\\k←1\mathrel{\&{to}}\\{dd}$ \1\&{do}\5
$\\{print}(\\{chr}(\\{module}[\\k]))$;\2\6
\45: \37\&{for} $\\k←1\mathrel{\&{to}}\\{dd}$ \1\&{do}\5
$\\{print}(\\{chr}(\\{out_contrib}[\\k]))$;\2\6
\4\&{othercases} \37\&{begin} \37$\\{print}(\.{\'?\'})$;\5
$\\{read}(\\{tty},\39\\{ddt})$;\6
\&{end}\2\6
\&{endcases};\6
\&{end};\2\6
\&{end};\5
\\{GUBED}\par
\N151.  The main program.
We have defined plenty of procedures, and it is time to put the last
pieces of the puzzle in place. Here is where \.{TANGLE} starts, and
where
it ends.

\Y\P\&{begin} \37\\{initialize};\5
\X109:Initialize the input system\X;\6
\X152:Phase I: Read all the user's text and compress it into \\
{tok_mem}\X;\6
$\\{STAT}\\{max_tok_ptr}←\\{tok_ptr}$;\5
\\{TATS}\X98:Phase II: Output the contents of the compressed tables\X;\6
\4\\{end_of_TANGLE}: \37\&{if} $\\{string_ptr}>128$ \1\&{then}\5
$\\{print_nl}(\\{string_ptr}-128:0,\39\.{\'\ strings\ written\ to\
string\ pool\ file.\'})$;\2\6
\\{STAT}\X153:Print statistics about memory usage\X;\6
\\{TATS}\C{here files should be closed if the operating system requires
it}\6
\&{end}.\par

\M152. \P$\X152:Phase I: Read all the user's text and compress it into
\\{tok_mem}\X≡$\6
$\\{phase_one}←\\{true}$;\5
$\\{module_count}←0$;\6
\1\&{repeat} \37$\\{next_control}←\\{skip_ahead}$;\6
\&{while} $\\{next_control}=\\{new_module}$ \1\&{do}\5
\\{scan_module};\2\6
\4\&{until}\5
\\{input_has_ended};\2\6
$\\{phase_one}←\\{false}$\par
\U section 151.

\M153. \P$\X153:Print statistics about memory usage\X≡$\6
$\\{ex_print_nl}(\.{\'Memory\ usage\ statistics:\'})$;\5
$\\{ex_print_nl}(\\{name_ptr}:0,\39\.{\'\ names,\ \'},\39\\
{text_ptr}:0,\39\.{\'\ replacement\ texts;\'})$;\5
$\\{ex_print_nl}(\\{byte_ptr}:0,\39\.{\'\ bytes,\ \'},\39\\
{max_tok_ptr}:0,\39\.{\'\ tokens.\'})$\par

\U section 151.
\N154.  Index.
If you have read and understood the code for Phase III in \.{WEAVE},
you
know what is in this index and how it got here. All modules in which an
identifier is used are listed with that identifier, except that
reserved
words are indexed only when they appear in format definitions, and the
appearances of identifiers in module names are not indexed. Underlined
entries correspond to where the identifier was declared.
\inx
\+\\{a}, \[73], \[135].
\+\\{abs}, 89, 128.
\+\\{accumulator}, \[128], 129, 130.
\+\\{add_in}, \[128], 129.
\+\\{and_sign}, \[100].
\+\\{app}, \[85], 87, 88, 89.
\+\\{app_repl}, \[79], 135, 136, 137, 138.
\+\\{app_val}, \[85], 89, 97.
\+\\{ascii_code}, 11, 14, 15, 27, 37, 52, 80, 81, 86, 112, 114, 135.
\+\\{ascii_file}, 11, 12.
\+\\{bad_case}, \[93], 95, 96.
\+\\{bal}, 73, 79, \[114], 115, 135, 136.
\+\&{begin}, \[2], 3, \[18].
\+\\{begin_comment}, \[59], 63, 106, 112, 118.
\+\\{begin_pascal}, \[112], 127, 129, 137, 144.
\+\\{boolean}, 10, 15, 16, 108, 148.
\+\\{brace_level}, 68, 69, 106.
\+\\{break_ptr}, 80, 81, 82, 83, 84, 88, 92, 93, 95, 96, 97.
\+\\{breakpoint}, \[150].
\+\\{buf_size}, 7, 14, 15, 19, 37, 40, 108.
\+\\{buffer}, 14, 15, 19, 20, 37, 40, 41, 43, 44, 45, 48, 51, 108, 109,
110, 111, 113, 114, 115, 117, 118, 119, 120, 121, 124, 125, 137, 138.
\+\\{byte_field}, 65, 66.
\+\\{byte_mem}, 26, 27, 28, 29, 35, 36, 40, 43, 48, 50, 53, 54, 55, 56,
62, 76, 99, 102.
\+\\{byte_ptr}, 28, 29, 30, 31, 48, 54, 76, 77, 153.
\+\\{byte_start}, 26, 27, 28, 29, 30, 31, 36, 37, 43, 48, 50, 54, 55,
62, 65, 67, 76, 102, 116.
\+\\{c}, \[40], \[53], \[56], \[113], \[117].
\+\\{carriage_return}, \[15], 108, 110.
\+\\{char}, 11.
\+\\{check_break}, \[83], 87, 88, 89, 97.
\+\\{chop_hash}, 37, 39, 47, 49.
\+\\{chopped_id}, 37, 40, 45, 50.
\+\\{chr}, 11, 20, 22, 36, 50, 51, 62, 63, 83, 126, 137, 150.
\+\\{compress}, \[118].
\+\\{confusion}, \[24], 75.
\+\\{continue}, \[5], 99, 135, 137, 142, 143.
\+\\{control_code}, \[112], 113, 116, 121.
\+\\{count}, 56.
\+\\{cur_byte}, 65, \[66], 69, 70, 71, 73, 76, 79.
\+\\{cur_char}, \[99], 102, 103, 104, 105.
\+\\{cur_end}, 65, \[66], 69, 70, 71, 73, 76.
\+\\{cur_module}, 116, 122, 137, 144.
\+\\{cur_name}, 65, \[66], 69, 70, 71.
\+\\{cur_repl}, 65, \[66], 69, 70, 71.
\+\\{cur_repl_text}, 134, 135, 139, 147.
\+\\{cur_repl_txt}, 144.
\+\\{cur_state}, 66, 70, 71.
\+\\{cur_val}, 72, 73, 75, 102, 104, 106.
\+\\{dd}, 148, 150.
\+\\{ddt}, 148, 149, 150.
\+\\{DEBUG}, 4, 17, 18, 61, 73, 76, 77, 117, 148, 149, 150.
\+\&{debug}, \[3].
\+\\{debug_help}, \[17], 18, 73, 117, 148, \[150].
\+\\{decr}, \[6], 71, 77, 79, 85, 102, 106, 115, 119, 124, 135, 136.
\+\\{define_macro}, \[139], 142, 143.
\+\\{definition}, \[112], 127, 129, 137, 142.
\+\\{digits}, \[104], 129.
\+\\{do_nothing}, \[6], 79, 88, 99, 111, 117, 129, 135, 150.
\+\\{done}, \[5], 73, 79, 113, 117, 124, 125, 128, 129, 130, 135, 137,
141, 142.
\+\\{double_chars}, 37, 51, 116, 120.
\+\\{double_dot}, \[59], 100, 118.
\+\&{drofnats}, \[3].
\+\\{DROFNATS}, 111.
\+\\{eight_bits}, 26, 27, 40, 68, 73, 81, 87, 99, 112, 113, 114, 117,
127, 135, 139.
\+\&{else}, 6.
\+\&{end}, 3, 6.
\+\\{end_comment}, \[59], 63, 106, 112, 118.
\+\\{end_field}, 65, 66.
\+\\{end_of_definition}, \[127], 130.
\+\\{end_of_TANGLE}, \[2], 23, 151.
\+\&{endcases}, \[6].
\+\\{eof}, 10, 13, 15.
\+\\{equal}, 53, 54, 55, 56.
\+\\{equiv}, 26, 27, 35, 37, 47, 49, 50, 51, 70, 74, 75, 76, 128, 129,
139, 147.
\+\\{equivalence_sign}, \[100], 118, 142, 143, 145.
\+\\{err_print}, \[18], 46, 56, 84, 99, 103, 105, 106, 120, 121, 129,
135, 138, 150.
\+\\{error}, 15, \[18], 50, 74, 76.
\+\\{ex_app}, \[85], 88, 97.
\+\\{ex_err_print}, \[18], 24, 25, 46, 53, 83, 114, 115, 120, 124, 125,
128, 130, 136, 137, 138, 142, 143, 145.
\+\\{ex_print_nl}, \[9], 13, 15, 18, 50, 74, 76, 98, 109, 126, 153.
\+\\{exit}, \[5], 6, 71, 93, 114, 141.
\+\\{extension}, 53, 55, 56.
\+\\{false}, 15, 16, 109, 152.
\+\\{flush_buffer}, \[83], 84.
\+\\{form_feed}, \[15], 108, 110, 111, 113, 114, 117, 124.
\+\\{format}, \[112], 127, 129, 137, 142.
\+\\{forward}, 17.
\+\\{found}, \[5], 40, 42, 43, 53, 56, 135, 138.
\+\\{frac}, \[86], 87, 88, 90, 99, 105.
\+\\{get}, 10, 15.
\+\\{get_fraction}, \[99], 104, 105.
\+\\{get_line}, \[110], 113, 114, 117, 124.
\+\\{get_next}, 116, \[117], 127, 129, 130, 131, 132, 135, 142, 143,
145.
\+\\{get_output}, 72, \[73], 80, 98, 99, 103, 104, 105.
\+\\{greater}, 53, 55, 56.
\+\\{greater_or_equal}, \[100], 118.
\+\&{gubed}, \[3].
\+\\{GUBED}, 4, 17, 18, 61, 73, 76, 77, 117, 148, 149, 150.
\+\\{hash}, 28, 37, 39, 42.
\+\\{hash_size}, 7, 37, 38, 39, 40, 41, 45.
\+\\{id_first}, 37, 40, 41, 43, 44, 45, 48, 51, 116, 119, 120.
\+\\{id_loc}, 37, 40, 41, 43, 45, 48, 51, 116, 119, 120.
\+\\{id_lookup}, 37, \[40], 116, 129, 137, 139, 142.
\+\\{ident}, \[86], 87, 88, 91, 100, 102.
\+\\{identifier}, \[72], 75, 102, 116, 119, 120, 129, 137, 142.
\+\\{ignore}, \[112], 113, 121.
\+\\{ilk}, 26, 27, 34, 35, 37, 44, 46, 47, 48, 51, 71, 75, 76, 129.
\+\\{incr}, \[6], 15, 41, 43, 45, 48, 50, 51, 54, 55, 56, 61, 62, 70,
73, 76, 79, 83, 85, 102, 103, 105, 106, 110, 113, 114, 115, 117, 118,
119, 120, 121, 124, 125, 135, 138, 141.
\+\\{initialize}, \[2], 151.
\+\\{input}, 2, 10, 15, 109.
\+\\{input_has_ended}, 108, 109, 110, 152.
\+\\{input_ln}, \[15], 110, 111.
\+\\{integer}, 72, 81, 85, 92, 93, 99, 128.
\+\\{join}, \[59], 99, 112.
\+\\{k}, \[36], \[61], \[83], \[85], \[87], \[150].
\+\\{last_unnamed}, 57, 58, 147.
\+\\{left_arrow}, \[100], 118.
\+\\{length}, \[28], 42.
\+\\{less}, 53, 54, 55, 56.
\+\\{less_or_equal}, \[100], 118.
\+\\{limit}, 15, 20, 108, 109, 110, 111, 113, 114, 117, 120, 124, 138.
\+\\{line}, 19, 20, 22, 82, 83, 108, 109, 110.
\+\\{line_length}, 7, 80, 83, 86, 87, 99, 103, 105.
\+\\{link}, 26, 27, 28, 35, 37, 42, 57.
\+\\{llink}, \[35], 53, 54, 56.
\+\\{loc}, 19, 20, 108, 109, 110, 113, 114, 115, 117, 118, 119, 120,
121, 124, 125, 130, 137, 138, 142.
\+\\{longest_name}, 7, 52, 53, 56, 117, 124, 126.
\+\&{loop}, \[6].
\+\\{macro}, 79.
\+\\{max_bytes}, 7, 27, 29, 36, 40, 48, 53, 54, 56, 76, 99.
\+\\{max_id_length}, 7, 102.
\+\\{max_names}, 7, 27, 28, 48, 54, 56, 76.
\+\\{max_texts}, 7, 27, 31, 57, 76, 135.
\+\\{max_tok_ptr}, 32, 77, 151, 153.
\+\\{max_toks}, 7, 27, 32, 60, 61, 79.
\+\\{misc}, \[81], 82, 84, 86, 87, 88, 91, 93, 97, 99, 104, 106.
\+{mod}, 80.
\+\\{mod_lookup}, 52, \[53], 122, 123.
\+\\{module}, 52, 53, 54, 55, 56, 117, 122, 123, 124, 125, 126, 150.
\+\\{module_count}, 140, 141, 146, 152.
\+\\{module_flag}, \[57], 71, 147.
\+\\{module_name}, \[112], 116, 121, 127, 129, 130, 134, 137, 142, 144.
\+\\{module_number}, \[72], 73, 106.
\+\\{name_field}, 65, 66.
\+\\{name_pointer}, 28, 29, 36, 40, 53, 56, 65, 70, 116, 128, 139, 141.
\+\\{name_ptr}, 28, 29, 30, 31, 36, 40, 42, 44, 46, 48, 54, 76, 77, 78,
79, 153.
\+\\{new_module}, 72, \[112], 127, 129, 137, 152.
\+\\{next_control}, 127, 129, 130, 131, 132, 134, 135, 142, 143, 144,
145, 152.
\+\\{next_sign}, 128, 129.
\+\&{nil}, 6.
\+\\{normal}, \[34], 37, 40, 44, 46, 47, 48, 75, 129, 137.
\+\\{not_equal}, \[100], 118.
\+\\{not_found}, \[5], 40, 50.
\+\\{not_sign}, \[100].
\+\\{num_or_id}, \[81], 87, 88, 93, 97.
\+\\{number}, \[72], 75, 104.
\+\\{numeric}, \[34], 40, 51, 75, 129, 142.
\+\\{octal}, \[59], 63, 104, 112, 129.
\+\\{open_input}, \[10], 109.
\+\\{or_sign}, \[100].
\+\\{ord}, 11, 15.
\+\&{othercases}, \[6].
\+\\{others}, 6.
\+\\{out_app}, 81, 88, 90, 92, 94.
\+\\{out_buf}, 21, 22, 80, 81, 82, 83, 84, 85, 86, 95, 96.
\+\\{out_buf_size}, 7, 21, 80, 83, 85.
\+\\{out_contrib}, 86, 87, 91, 99, 100, 102, 103, 104, 105, 150.
\+\\{out_ptr}, 21, 22, 80, 81, 82, 83, 84, 85, 88, 92, 93, 95, 96, 97.
\+\\{out_sign}, 81, 89, 90, 93, 94.
\+\\{out_state}, 81, 82, 84, 87, 88, 90, 92, 93, 94, 97, 99, 103.
\+\\{out_val}, 81, 89, 90, 92, 93, 94.
\+\\{output}, 2.
\+\\{output_state}, 65, 66.
\+\\{overflow}, \[25], 48, 54, 60, 70, 76, 79, 135.
\+\\{p}, \[139], \[141].
\+\\{pack}, 48.
\+\\{page}, 19, 20, 108, 109, 110, 111.
\+\\{page_end}, \[112], 113, 117, 127, 129, 137, 145.
\+\\{param}, \[59], 63, 73, 79, 135.
\+\\{parametric}, \[34], 40, 71, 75, 134, 135, 143.
\+\\{phase_one}, 16, 18, 152.
\+\\{pool}, 2, 12, 13, 51.
\+\\{pop_level}, \[71], 73, 76, 77.
\+\\{prefix}, 53, 55, 56.
\+\\{prefix_lookup}, \[56], 122.
\+\\{print}, \[9], 20, 22, 36, 50, 61, 62, 63, 74, 79, 83, 110, 126,
150.
\+\\{print_id}, \[36], 62, 74, 76, 150.
\+\\{print_ln}, \[9], 20, 22.
\+\\{print_nl}, \[9], 98, 151.
\+\\{print_repl}, \[61], 150.
\+\&{procedure}, \[2].
\+\\{push_level}, \[70], 74, 75, 78.
\+\\{quit}, 2, 13, 18, \[23], 24, 25, 109.
\+\\{read}, 150.
\+\\{read_ln}, 10, 15.
\+\\{repl}, 34.
\+\\{repl_field}, 65, 66.
\+\\{reset}, 10.
\+\\{restart}, \[5], 73, 74, 75, 76, 78, 87, 88, 90, 117, 121.
\+\\{reswitch}, \[5], 99, 103, 104, 105, 128, 129.
\+\&{return}, \[6].
\+\\{rewrite}, 13.
\+\\{rlink}, \[35], 53, 54, 56.
\+\\{scan_module}, 140, \[141], 152.
\+\\{scan_numeric}, 127, \[128], 142.
\+\\{scan_repl}, 134, \[135], 139, 144.
\+\\{send_out}, 86, \[87], 98, 99, 100, 102, 103, 104, 105, 106.
\+\\{send_sign}, 86, \[92], 98, 99.
\+\\{send_the_output}, 98, \[99].
\+\\{send_val}, 86, \[93], 98, 104, 106.
\+\\{set_element_sign}, \[100].
\+\\{sign}, \[81], 88, 92, 94.
\+\\{sign_val}, \[81], 88, 90, 92, 93, 94.
\+\\{sign_val_sign}, \[81], 88, 92, 94.
\+\\{sign_val_val}, \[81], 88, 92, 94.
\+\\{simple}, \[34], 40, 75, 76, 134, 142.
\+\\{sixteen_bits}, 26, 27, 37, 53, 56, 60, 61, 65, 73, 87, 108, 135,
148, 150.
\+\\{skip_ahead}, \[113], 121, 130, 142, 145, 152.
\+\\{skip_comment}, \[114], 117.
\+\\{stack}, 65, 66, 70, 71.
\+\\{stack_ptr}, 65, 66, 69, 70, 71, 73, 76, 99, 103.
\+\\{stack_size}, 7, 66, 70.
\+\\{STANFORD}, 111.
\+\&{stanford}, \[3].
\+\&{stat}, \[3].
\+\\{STAT}, 32, 77, 151.
\+\\{store_two_bytes}, \[60], 79, 146.
\+\\{str}, \[86], 87, 100, 103, 104.
\+\\{string_ptr}, 28, 29, 30, 51, 151.
\+{system dependencies}, \[1], 2, 3, 4, 6, 8, 9, 10, 11, 13, 15, 100,
150, 151.
\+\\{tab_mark}, \[15], 20, 112, 115, 117, 124, 125.
\+\\{TANGLE}, \[2].
\+\&{tats}, \[3].
\+\\{TATS}, 32, 77, 151.
\+\\{TEX_string}, \[112], 121.
\+\\{text_link}, 26, 27, 31, 57, 58, 65, 69, 71, 76, 98, 139, 147.
\+\\{text_pointer}, 31, 32, 57, 61, 65, 134.
\+\\{text_ptr}, 31, 32, 33, 61, 67, 76, 77, 135, 153.
\+\\{tok_mem}, 26, 27, 31, 32, 57, 60, 61, 62, 65, 66, 67, 73, 76, 79,
135.
\+\\{tok_ptr}, 31, 32, 33, 60, 67, 76, 77, 79, 135, 151.
\+\\{tok_start}, 26, 27, 31, 32, 33, 57, 61, 65, 69, 70, 71, 76, 77,
135.
\+\\{trouble_shooting}, 73, 117, 148, 149.
\+\\{true}, 6, 10, 15, 16, 108, 110, 148, 149, 152.
\+\\{tty}, 2, 9, 150.
\+\\{unambig_length}, 7, 34, 37, 40, 45, 50.
\+\\{unbreakable}, \[81], 88, 99, 103.
\+\\{up_to}, \[102], 117.
\+\\{val}, 128, 129, 131, 132.
\+\\{write}, 9, 51, 83.
\+\\{write_ln}, 9, 83.
\+\&{xclause}, 6.
\fin